using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication6
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button3;
public static ArrayList tt=new ArrayList();
//**********************************************************
public static void EventTrigged(object sender, MyEventArgs e)
{
MessageBox.Show("您输入的是" + e.I + ",事件被触发。");
}
public static void EventTrigged2(object sender, MyEventArgs2 e)
{
MessageBox.Show("您输入" + e.J + ",事件被触发。");
}
//**********************************************************
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "模拟赋值int";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 23);
this.button2.TabIndex = 2;
this.button2.Text = "模拟赋值string";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(128, 104);
this.button3.Name = "button3";
this.button3.TabIndex = 3;
this.button3.Text = "button3";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(464, 273);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//*********************
EventClass ee= new EventClass();
// 指定事件的处理程序
ee.MyEvent += new MyEventHandler(EventTrigged);
ee.I=Convert.ToInt32(this.textBox1.Text.ToString().Trim());
//*********************
}
private void button2_Click(object sender, System.EventArgs e)
{
//*********************
EventClass2 ee= new EventClass2();
// 指定事件的处理程序
ee.MyEvent2 += new MyEventHandler2(EventTrigged2);
ee.J=this.textBox1.Text.ToString().Trim();
//*********************
}
private void button3_Click(object sender, System.EventArgs e)
{
MessageBox.Show( Form1.tt.Count.ToString());
}
}
//**********************************************************
// 创建一个类,用于传递事件的数据,该类从EventArgs继承
public class MyEventArgs:EventArgs
{
public int I;
public MyEventArgs(int i)
{
I=i;
}
}
public class MyEventArgs2:EventArgs{
public string J;
public MyEventArgs2(string j){J=j;}
}
// 定义一个delegate,该delegate返回void,有两个参数:object sender, EventArgs e
delegate void MyEventHandler(object sender, MyEventArgs e);
delegate void MyEventHandler2(object sender, MyEventArgs2 e);
///
class EventClass
{
// 定义事件,delegate是刚刚定义的delegate
public event MyEventHandler MyEvent;
private int i;
public int I
{
get{return i;}
set
{
i=value;
if (!value.Equals("!@##$#$%$^^&&&^&*&*&*"))
{
// 如果将I的值改为100,则触发MyEvent事件的发生
if (MyEvent != null)
{
MyEventArgs args = new MyEventArgs(value); // 定义一个EventArgs对象
MyEvent(this, args); // 触发事件
}
}
}
}
}
class EventClass2
{
// 定义事件,delegate是刚刚定义的delegate
public event MyEventHandler2 MyEvent2;
private string j;
private ArrayList jj=new ArrayList();
public String J
{
get{
if(jj.Count==0){return "没有任何数据";}
return jj[jj.Count-1].ToString();
}
set{
jj.Add(value.ToString());
Form1.tt.Add(value.ToString());
if (!value.Equals("!@##$#$%$^^&&&^&*&*&*"))
{
// 如果将I的值改为100,则触发MyEvent事件的发生
if (MyEvent2 != null)
{
MyEventArgs2 args2 = new MyEventArgs2(value); // 定义一个EventArgs对象
MyEvent2(this, args2); // 触发事件
}
}
}
}
}
//**********************************************************
}