建立事件(event)

來源:互聯網
上載者:User

事件(event):

讓我通過一個例子來類比事件的整個過程:

  1. 建立一個button類,它裡面有一個click 事件。
  2. 建立一個Form類,他裡面有一個我們上面定義的button類。
  3. 要求:當我們使用者單擊button類的時候From類要對他進行處理,輸出一條資訊“我知道你被單擊了”

請看:


首先我們會單擊button,然後button會通知Form,然後From就作出相應。這個過程在C#裡面應該怎麼做到呢?

下面我會列出上述例子的原始碼(這裡就不介紹怎麼聲明event等等內容了):

using System;
using System.Collections.Generic;
using System.Text;

namespace UsingEvent
{
    public delegate voidClickEventHandler(object sender, EventArgs e);//聲明一個代表:請看文章最後面Note

    public classMyButton             //建立MyBottom
    {
        public event ClickEventHandler ClickEvent;//聲明一個事件

        public voidClick()                                //單擊MyButton
        {
            if (ClickEvent != null)  //當myButton.ClickEvent += newClickEventHandler(OnClickEvent);   時成立
            {
               Console.WriteLine("MyButton: 我被單擊了");
               ClickEvent(this,null);                         //拋出事件,給所有相應者
            }
        }
    }

    public class MyForm
    {
        public MyButton myButton = newMyButton();

        public MyForm()
        {

            //添加事件到myButton中,當myButton被單擊的時候就會調用相應的處理函數

            myButton.ClickEvent += new ClickEventHandler(OnClickEvent);   

         }

       //事件處理函數

       void
OnClickEvent(object sender, EventArgs e)
        {
           Console.WriteLine("MyForm: 我知道你被單擊了!");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyForm form= new MyForm();//產生一個MyForm

           form.myButton.Click();//單擊MyForm中的滑鼠,效果就出來了
        }
    }
}
 

Note:public delegate void ClickEventHandler(object sender, EventArgs e);這是事件委託標準的聲明方法,其實在參數裡面我們可以不傳,也可以是其他類型的。但是最好還是使用上面的聲明方法,你可以繼承EventArgs,來封裝你要傳送的其他任何參數。

/////////////////////////////////////////////////////////////////////////////

分享至:http://www.cnblogs.com/scottckt/archive/2010/09/29/1838598.html

謝謝原作者

例2:說明EventArgs傳參數的方法:

//列印對象
        public class CustomPrint
        {
            /// <summary>
            /// 1、定義事件參數
            /// </summary>
            public class CustomPrintArgument : EventArgs
            {
                private int copies;
                public CustomPrintArgument(int numberOfCopies)
                {
                    this.copies = numberOfCopies;
                }
                public int Copies
                {
                    get { return this.copies; }
                }
            }

            /// <summary>
            /// 2、聲明事件的委託
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public delegate void CustomPrintHandler(object sender, CustomPrintArgument e);

            /// <summary>
            /// 3、聲明事件
            /// </summary>
            public event CustomPrintHandler CustomPrintEvent;

            /// <summary>
            /// 4、定義觸發事件
            /// </summary>
            /// <param name="copyies">份數</param>
            public void RaisePrint(int copyies)
            {
                CustomPrintArgument e = new CustomPrintArgument(copyies);
                CustomPrintEvent(this, e);
            }
        }
        /// <summary>
        /// WinForm 建構函式
        /// </summary>
        public Form1()
        {
            InitializeComponent();

            PrintCustom();
        }

        /// <summary>
        /// 列印方法
        /// </summary>
        private void PrintCustom()
        {
            //執行個體對象
            CustomPrint cp = new CustomPrint();

            //添加事件
            cp.CustomPrintEvent += new CustomPrint.CustomPrintHandler(cp_CustomPrintEvent);

            //5、觸發事件
            cp.RaisePrint(10);

        }

        //6、事件處理
        void cp_CustomPrintEvent(object sender, CustomPrint.CustomPrintArgument e)
        {
            int copies = e.Copies;
            MessageBox.Show(copies.ToString());
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.