程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 單參數委托通用類C#

單參數委托通用類C#

編輯:C#入門知識

這是我剛完成的一個單參數委托通用類,主要目的是為了實驗委托與事件的使用,測試結果很成功。(發現時間已經是深夜1點多了,汗~~~)

現在我把整個代碼貼出來,給大家,如有良策,還望賜教,謝謝。

 


 

view plaincopy to clipboardprint?<span style="font-family:Microsoft YaHei;font-size:18px;">using System; 
 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; 
 
namespace DawnXZ.Tools 

    /// <summary>  
    /// General commissioned a single parameter  
    /// </summary>  
    public class DelegateTsit 
    { 
        #region Delegate & Event  
        /// <summary>  
        /// Declare a delegate  
        /// </summary>  
        /// <param name="sender">The delegate object</param>  
        /// <param name="e">The delegate event</param>  
        public delegate void UpdateInfoEventHandler(object sender, UpdateInfoEventArgs e); 
        /// <summary>  
        /// Declare a delegate event  
        /// </summary>  
        private event UpdateInfoEventHandler UpdateInfo; 
        #endregion Delegate & Event 
 
        #region Delegate event class  
        /// <summary>  
        /// Delegate event class  
        /// </summary>  
        public class UpdateInfoEventArgs : EventArgs 
        { 
            /// <summary>  
            /// To update the information  
            /// </summary>  
            public readonly string _strInfo; 
            /// <summary>  
            /// System.Windows.Forms  
            /// </summary>  
            public readonly Form _form; 
            /// <summary>  
            /// Constructed function  
            /// </summary>  
            /// <param name="form">System.Windows.Forms</param>  
            /// <param name="strInfo">To update the information</param>  
            public UpdateInfoEventArgs(Form form, string strInfo) 
            { 
                this._strInfo = strInfo; 
                this._form = form; 
            } 
        } 
        #endregion Delegate event class 
 
        #region Register & UnRegister  
        /// <summary>  
        /// To register more than one method for an event  
        /// </summary>  
        /// <param name="method">Method name</param>  
        public void Register(UpdateInfoEventHandler method) 
        { 
            UpdateInfo += method; 
        } 
        /// <summary>  
        /// Registered with the event a unique method  
        /// </summary>  
        /// <param name="method">Method name</param>  
        public void RegisterOnly(UpdateInfoEventHandler method) 
        { 
            UpdateInfo = method; 
        } 
        /// <summary>  
        /// Registered with the event against method  
        /// </summary>  
        /// <param name="method">Method name</param>  
        public void UnRegister(UpdateInfoEventHandler method) 
        { 
            UpdateInfo -= method; 
        } 
        #endregion Register & UnRegister 
 
        #region Virtual  
        /// <summary>  
        /// Can be overridden execute method  
        /// </summary>  
        /// <param name="form">System.Windows.Forms</param>  
        /// <param name="e">The delegate event</param>  
        protected virtual void OnUpdateInfo(Form form, UpdateInfoEventArgs e) 
        { 
            if (UpdateInfo != null) form.Invoke(UpdateInfo, this, e); 
        } 
        #endregion Virtual 
 
        #region Member method  
        /// <summary>  
        /// Executes a delegate method  
        /// </summary>  
        /// <param name="form">System.Windows.Forms</param>  
        /// <param name="strInfo">To update the information</param>  
        public void Executes(Form form, string strInfo) 
        { 
            UpdateInfoEventArgs e = new UpdateInfoEventArgs(form, strInfo); 
            OnUpdateInfo(form, e); 
            e = null; 
        } 
        #endregion Member method  
    } 

</span> 
<span style="font-family:Microsoft YaHei;font-size:18px;">using System;

using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DawnXZ.Tools
{
    /// <summary>
    /// General commissioned a single parameter
    /// </summary>
    public class DelegateTsit
    {
        #region Delegate & Event
        /// <summary>
        /// Declare a delegate
        /// </summary>
        /// <param name="sender">The delegate object</param>
        /// <param name="e">The delegate event</param>
        public delegate void UpdateInfoEventHandler(object sender, UpdateInfoEventArgs e);
        /// <summary>
        /// Declare a delegate event
        /// </summary>
        private event UpdateInfoEventHandler UpdateInfo;
        #endregion Delegate & Event

        #region Delegate event class
        /// <summary>
        /// Delegate event class
        /// </summary>
        public class UpdateInfoEventArgs : EventArgs
        {
            /// <summary>
            /// To update the information
            /// </summary>
            public readonly string _strInfo;
            /// <summary>
            /// System.Windows.Forms
            /// </summary>
            public readonly Form _form;
            /// <summary>
            /// Constructed function
            /// </summary>
            /// <param name="form">System.Windows.Forms</param>
            /// <param name="strInfo">To update the information</param>
            public UpdateInfoEventArgs(Form form, string strInfo)
            {
                this._strInfo = strInfo;
                this._form = form;
            }
        }
        #endregion Delegate event class

        #region Register & UnRegister
        /// <summary>
        /// To register more than one method for an event
        /// </summary>
        /// <param name="method">Method name</param>
        public void Register(UpdateInfoEventHandler method)
        {
            UpdateInfo += method;
        }
        /// <summary>
        /// Registered with the event a unique method
        /// </summary>
        /// <param name="method">Method name</param>
        public void RegisterOnly(UpdateInfoEventHandler method)
        {
            UpdateInfo = method;
        }
        /// <summary>
        /// Registered with the event against method
        /// </summary>
        /// <param name="method">Method name</param>
        public void UnRegister(UpdateInfoEventHandler method)
        {
            UpdateInfo -= method;
        }
        #endregion Register & UnRegister

        #region Virtual
        /// <summary>
        /// Can be overridden execute method
        /// </summary>
        /// <param name="form">System.Windows.Forms</param>
        /// <param name="e">The delegate event</param>
        protected virtual void OnUpdateInfo(Form form, UpdateInfoEventArgs e)
        {
            if (UpdateInfo != null) form.Invoke(UpdateInfo, this, e);
        }
        #endregion Virtual

        #region Member method
        /// <summary>
        /// Executes a delegate method
        /// </summary>
        /// <param name="form">System.Windows.Forms</param>
        /// <param name="strInfo">To update the information</param>
        public void Executes(Form form, string strInfo)
        {
            UpdateInfoEventArgs e = new UpdateInfoEventArgs(form, strInfo);
            OnUpdateInfo(form, e);
            e = null;
        }
        #endregion Member method
    }
}
</span>

 

以下代碼為 From 窗體中的:

 

 

view plaincopy to clipboardprint?<span style="font-family:Microsoft YaHei;font-size:18px;"> 
#region Form event  
        /// <summary>  
        /// Form Loading  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void frmMain_Load(object sender, EventArgs e) 
        { 
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(IamTest)); 
            thread.Start(); 
            thread.IsBackground = true; 
        } 
        private void IamTest() 
        { 
            int i = 0; 
            while (true) 
            { 
                i++; 
                DawnXZ.Tools.DelegateTsit delUpdate = new DawnXZ.Tools.DelegateTsit(); 
                delUpdate.Register(UpdateTxtInfoMsg); 
                delUpdate.Executes(this, "I am a test information."); 
                if (i > 100) break; 
                System.Threading.Thread.Sleep(2000); 
            } 
        } 
        /// <summary>  
        /// Close the form  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void frmMain_Closing(object sender, CancelEventArgs e) 
        { 
 
        } 
        /// <summary>  
        /// Closed form  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void frmMain_Closed(object sender, EventArgs e) 
        { 
 
        } 
        #endregion Form event 
 
 
#region Update logs information  
        /// <summary>  
        /// Update logs information  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void UpdateTxtInfoMsg(object sender, DawnXZ.Tools.DelegateTsit.UpdateInfoEventArgs e) 
        { 
            //this._logger.Write(strInfo);  
            if (txtInfoMsg.Text.Length > 65535) 
            { 
                txtInfoMsg.Text = string.Empty; 
            } 
            txtInfoMsg.Text = string.Format("{0}  {1}\r\n{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), e._strInfo, txtInfoMsg.Text); 
        } 
        #endregion Update logs information  
 
 
 
 
</span> 
<span style="font-family:Microsoft YaHei;font-size:18px;">
#region Form event
        /// <summary>
        /// Form Loading
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(IamTest));
            thread.Start();
            thread.IsBackground = true;
        }
        private void IamTest()
        {
            int i = 0;
            while (true)
            {
                i++;
                DawnXZ.Tools.DelegateTsit delUpdate = new DawnXZ.Tools.DelegateTsit();
                delUpdate.Register(UpdateTxtInfoMsg);
                delUpdate.Executes(this, "I am a test information.");
                if (i > 100) break;
                System.Threading.Thread.Sleep(2000);
            }
        }
        /// <summary>
        /// Close the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Closing(object sender, CancelEventArgs e)
        {

        }
        /// <summary>
        /// Closed form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Closed(object sender, EventArgs e)
        {

        }
        #endregion Form event


#region Update logs information
        /// <summary>
        /// Update logs information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateTxtInfoMsg(object sender, DawnXZ.Tools.DelegateTsit.UpdateInfoEventArgs e)
        {
            //this._logger.Write(strInfo);
            if (txtInfoMsg.Text.Length > 65535)
            {
                txtInfoMsg.Text = string.Empty;
            }
            txtInfoMsg.Text = string.Format("{0}  {1}\r\n{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), e._strInfo, txtInfoMsg.Text);
        }
        #endregion Update logs information

 


</span>


 作者“蟑螂·魂——晨曦小竹”

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved