图1 带EventArgs的委托演示
图2 不带EventArgs的委托演示
带EventArgs的委托演示例程源代码如下:
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;
using System.Collections;
using System.Diagnostics;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public static TextBox ttt = null;
public Form1()
{
InitializeComponent();
ttt = this.textBox1;
}
p rivate void button1_Click(object sender, EventArgs e)
{
employee employee1 = new employee();
employee1.Age = 33;
employee1.Name = "猪悟能";
admin admin1 = new admin();
employee1.playgame +=new delegate1(admin1.notify);
employee1.games();
}
p rivate class employee
{
String name = "";
int age = 0;
public event delegate1 playgame;
public String Name
{
get { return this.name; }
set { this.name = value; }
}
public int Age
{
get { return this.age; }
set { this.age = value; }
}
public void games()
{
if (playgame != null)
{
custom e = new custom();
e.Age = this.age;
e.Name = this.name;
playgame(this,e);
}
}
}
p rivate class admin
{
public void notify(object sender,custom e)
{
Form1.ttt.Text =e.Name+ 在玩游戏!年龄:"+e.Age.ToString()+"宀?;
}
}
public delegate void delegate1(object sender,custom e);
public class custom : EventArgs
{
String name = "";
int age = 0;
public String Name
{
set{this.name = value;}
get { return this.name; }
}
public int Age
{
set { this.age = value; }
get { return this.age; }
}
}
}
}