C#委托事件定义调用实例 .

最近学习了委托与事件的使用,为了加深了解对事件认识,下面使用委托事件方式,新建个winform,施一个button1,一个textbox1,设置TextBox.Text实例,在vs2010下测试通过

 

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

namespace 事件实例
{
    public partial class Form1 : Form
    {
        delegate void MyDeleGate(string s); //1.定义委托结构
        event MyDeleGate MyEvent; //2.定义事件

        public Form1()
        {
            InitializeComponent();

            MyEvent += new MyDeleGate(Form1_MyEvent); //3.梆定事件方法
        }

        //4.自动生成的事件方法
        void Form1_MyEvent(string s)
        {
            textBox1.Text = s;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //5.调用事件
            MyEvent("那是一阵风");
        }
                
    }
}

 

posted @ 2013-12-20 15:51  那是一阵风  阅读(170)  评论(0)    收藏  举报