代码改变世界

学习实践:委托与事件的小例子

2011-05-29 08:12  秋日愚夫  阅读(559)  评论(0编辑  收藏  举报

听说委托这个槛是比较难过的。。。所以在这方面读了很多相关文档,下面的这个小例子,就是这几天的学习成果。。。。

PS:只是给像我一样刚入门的朋友看的,大侠飘过。。。

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 StudyDeleget
{
    public delegate void SaySomeThing (string name);   // 声明委托

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();       

        }

        public void Hello(string name)    //方法一
        {
            txtWrite.Text += name + "Say Hello! \r\n";
        }

        public void NiceToSeeYou(string name)  //方法二
        {
            txtWrite.Text += name + "Say nice to see you ! \r\n";
        }

        public event SaySomeThing SomeBodySay;   //将要绑定委托的事件

        private void button1_Click(object sender, EventArgs e)
        {
            txtWrite.Text = "";
            string name = txtName.Text;

            SaySomeThing SayHello = new SaySomeThing(Hello);         //实例化委托
            SaySomeThing SayNice = new SaySomeThing(NiceToSeeYou);

            SomeBodySay += SayHello;   //向事件绑定委托方法
            SomeBodySay += SayNice;

            SomeBodySay -= SayNice;   

            SomeBodySay(name);  //触发事件
        }
    }
}
 Designer.cs
namespace StudyDeleget
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.txtWrite = new System.Windows.Forms.TextBox();
this.btnSayChinese = new System.Windows.Forms.Button();
this.txtName = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtWrite
//
this.txtWrite.Location = new System.Drawing.Point(163, 12);
this.txtWrite.Multiline = true;
this.txtWrite.Name = "txtWrite";
this.txtWrite.Size = new System.Drawing.Size(419, 333);
this.txtWrite.TabIndex = 0;
//
// btnSayChinese
//
this.btnSayChinese.Location = new System.Drawing.Point(12, 54);
this.btnSayChinese.Name = "btnSayChinese";
this.btnSayChinese.Size = new System.Drawing.Size(133, 23);
this.btnSayChinese.TabIndex = 1;
this.btnSayChinese.Text = "button1";
this.btnSayChinese.UseVisualStyleBackColor = true;
this.btnSayChinese.Click += new System.EventHandler(this.button1_Click);
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(12, 12);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(133, 21);
this.txtName.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(594, 357);
this.Controls.Add(this.txtName);
this.Controls.Add(this.btnSayChinese);
this.Controls.Add(this.txtWrite);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox txtWrite;
private System.Windows.Forms.Button btnSayChinese;
private System.Windows.Forms.TextBox txtName;
}
}