C#_自定义事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Winform_EvnetTest
{
    public partial class Form1 : Form
    {
        // 1.自定义事件
        public class TextChangeEventArgs : EventArgs
        {
            private string message;
            public TextChangeEventArgs(string message)
            {
                this.message = message;
            }

            public string Message
            {
                get { return message; }
            }
        }

        //2.1 定义委托
        public delegate void TextBoxChangedHandle(object sender, TextChangeEventArgs e);
        //2.2 定义事件
        public event TextBoxChangedHandle UserControlValueChanged;
        //2.3 
        private void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            if (UserControlValueChanged != null)
                UserControlValueChanged(this, new TextChangeEventArgs(this.textBox1.Text));

            MessageBox.Show(this.textBox1.Text);
        }

        public Form1()
        {
            InitializeComponent();

            //3.注册事件
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
        }
    }
}

你可能感兴趣的:(C#_自定义事件)