Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12471484
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2014-04-11 21:32:17

    下面先通过一个案例来说明事件的声明、赋值、触发过程。

一、窗体创建及事件编写

image

1、父窗体相关


  1. namespace TestEvent
  2. {
  3.     public partial class MainForm : Form
  4.     {
  5.         public bool MIsSonFormVisible = false;
  6.         private SonForm mSonForm;
  7.         public MainForm()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.         private void button1_Click(object sender, EventArgs e)
  12.         {
  13.             if (!this.MIsSonFormVisible)
  14.             {
  15.                 this.mSonForm = new SonForm(this);
  16.                 this.mSonForm.Show();
  17.                 this.MIsSonFormVisible = true;
  18.             }
  19.         }
  20.         private void MainForm_Load(object sender, EventArgs e)
  21.         {
  22.             SonForm.SonEvent += new SonForm.DSonFormCmd(parseSonFormCmd);
  23.         }
  24.         private void parseSonFormCmd(bool isOk,string text)
  25.         {
  26.             if (isOk)
  27.             {
  28.                 this.textBox1.Text = "Son Says: " + text;
  29.             }
  30.         }
  31.     }
  32. }


2、子窗体相关


  1. namespace TestEvent
  2. {
  3.     public partial class SonForm : Form
  4.     {
  5.         public delegate void DSonFormCmd(bool isOk,string text);
  6.         static public event DSonFormCmd SonEvent;
  7.         public void OnSonEvent(bool isOk)
  8.         {
  9.             if (SonEvent != null)
  10.             {
  11.                 SonEvent(isOk,this.textBox1.Text);
  12.             }
  13.         }
  14.         private Form mFather;
  15.         public SonForm(Form father)
  16.         {
  17.             InitializeComponent();
  18.             this.mFather = father;
  19.         }
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             this.OnSonEvent(true);
  23.         }
  24.         private void SonForm_FormClosing(object sender, FormClosingEventArgs e)
  25.         {
  26.             ((MainForm)this.mFather).MIsSonFormVisible = false;
  27.         }
  28.     }
  29. }



二、关键语法说明

    首先明确,事件常作为一个类的成员出现。

    事件的声明及使用主要分为几个关键部分:声明事件委托、声明事件、添加事件触发方法、编写事件响应方法。

1、事件声明

(1)声明事件使用的委托;

  1. public delegate void DSonFormCmd(bool isOk,string text);

(2)声明一个事件,并指明它使用的委托的类型。

  1. static public event DSonFormCmd SonEvent;

(3)编写事件触发方法

  1. public void OnSonEvent(bool isOk) 
  2. {
  3.     if (SonEvent != null) 
  4.     {
  5.         SonEvent(isOk,this.textBox1.Text);
  6.     }
  7. }

    注意这里SonEvent(isOk,this.textBox1.Text);的值将被传到事件委托处理方法中。一执行OnSonEvent()则马上触发事件。


2、在其它类中使用事件

    使用事件首先要向其注册事件响应方法。然后编写好响应方法即可。

  1. SonForm.SonEvent += new SonForm.DSonFormCmd(parseSonFormCmd);
  2.         private void parseSonFormCmd(bool isOk,string text)
  3.         {
  4.             if (isOk)
  5.             {
  6.                 this.textBox1.Text = "Son Says: " + text;
  7.             }
  8.         }


再探总结:

    事件的声明及使用主要是主要按照这几个步骤进行,正常使用是OK的。待再作深探windows的事件机制。

参考博客:
http://www.cnblogs.com/huomm/archive/2007/12/04/982869.html

阅读(2014) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~