您的位置:首页 > 编程语言 > C#

C# 实现事件(Event)演习代码

2007-05-31 21:22 661 查看
using System;
using System.Collections.Generic;
using System.Text;

namespace Event
{
    class Program
    {
        //定义包含事件数据的类:
        public class myEventArgs : EventArgs  //***
        {
            private bool yes_or_no;
            private string txt;

            public myEventArgs()
            {
            }

            public myEventArgs(bool yn, string s)
            {
                YorN = yn;
                txt = s;
            }

            public bool YorN
            {
                get
                {
                    return yes_or_no;
                }
                set
                {
                    yes_or_no = value;
                }
            }

            public string Txt
            {
                get
                {
                    return Txt;
                }
                set
                {
                    txt = value;
                }
            }

            public string ReturnTxt()
            {
                if (yes_or_no)
                {
                    return "Hello: " + txt + " YES! YES! YES!";
                }
                else
                {
                    return "Hello: " + txt + " Not NO!";
                }
            }
        }

        /// <summary>
        /// 声名委托
        /// </summary>
        /// <param name="sender">表示事件的引发者</param>
        /// <param name="e">事件参数</param>
        public delegate void myEventHandler(Object sender, myEventArgs e);  //***

        //
        public class App
        {
            private bool yn;
            private string txt;

            public bool YorN
            {
                get { return yn;}
                set { yn = value; }
            }

            public string Text
            {
                get { return txt; }
                set { txt = value; }
            }

            public event myEventHandler myEvHdl;  //***

            protected virtual void OnFire(myEventArgs e)
            {
                if (myEvHdl != null)
                {
                    myEvHdl(this, e); //调用委托
                }
            }

            public void Run()
            {
                myEventArgs e = new myEventArgs(yn, txt);
                OnFire(e);
            }
        }

        public class Winform  //模拟 Windows 的 Form
        {
            public void DemoOnMouseClick(object sender, myEventArgs e)  //模拟鼠标点击事件
            {
                Console.WriteLine(e.ReturnTxt());
            }
        }

        /// <summary>
        /// 主类:程序的主流程
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Winform winform = new Winform();  //事件接收者
            App app = new App();  //事件源
            //绑定DemoOnMouseClick方法到事件myEvHdl
            app.myEvHdl += new myEventHandler(winform.DemoOnMouseClick);  //***

            app.YorN = true;
            app.Text = "网眼的 http://www.why100000.com!";
            app.Run();

            app.YorN = false;
            app.Text = "网眼的 http://why100000.com!";
            app.Run();

            Console.ReadLine();
        }
    }
}

电脑学习网:http://www.why100000.com
   张庆  2007.5.31
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐