码迷,mamicode.com
首页 > Windows程序 > 详细

模拟winform里的控件的事件和委托机制

时间:2015-03-03 13:13:37      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

 

下面的代码是我模块winform的事件机制,一看就懂。

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary.事件
{
    class 模拟控件事件机制
    {

    }

    public class Form1
    {
        MyButton button1;
        public Form1()
        {
            FormIni();
            button1.Text = "111";
        }
        public void FormIni()
        {
            this.button1 = new MyButton("button1");///
            button1.TextChanged += new MyButton.MyButtonTextChangedHandler(Button1OnTextChanged) ;
        }

        void Button1OnTextChanged(object send, MyButtonEventArgs e)
        {
            Console.WriteLine("button1的Text属性改变后调用,MyButtonEventArgs的message信息为:"+e.Message);
        }
    
    }

    /// <summary>
    /// 定义Button
    /// </summary>
    class MyButton
    {
        /// <summary>
        /// 按钮的Text
        /// </summary>
        private string text;    
        public string Text
        {
            get { return text; }
            set { text = value; OnTextChanged(new MyButtonEventArgs(name)); }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public MyButton(string name)
        {
            Name = name;        
        }

        ///定义事件的委托
        public delegate void MyButtonTextChangedHandler(object sender, MyButtonEventArgs e);

        ///定义事件
        public event MyButtonTextChangedHandler TextChanged;
        ///定义引发事件的方法OnTextChanged()
        private void OnTextChanged(MyButtonEventArgs e)
        {
            e.Message = name;          
            Console.WriteLine("MyButton的Text属性改变,MyButton控件的事件TextChanged发生了,转入信息:"+e.Message);
            TextChanged(this, e);///如果没有这一行,MyButton的Text改变后引起的其它操作就不能运行
        }

    }

    /// <summary>
    /// 自定义事件参数
    /// </summary>
    class MyButtonEventArgs
    {
        private string message;///

        public string Message
        {
            get { return message; }
            set { message = value; }
        }


        public MyButtonEventArgs(string message)
        {
            this.message = message;
        }

    }


   
}
View Code

 

模拟winform里的控件的事件和委托机制

标签:

原文地址:http://www.cnblogs.com/shengyu-kmust/p/4310658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!