谢谢了 小弟也没什么分了,给个10分,讨个写法?希望得到帮助
解决方案 »
- 算法大比拼,类型数组如何实现乱序排列
- 做C#项目时,属性怎么运用啊,每次都没怎么用,也能做出来
- 求提供asp.net代码,程序的网站,及文档
- 关于做OA方面的书,我用的是c#和.net
- 短信收发内容如何存到数据表?serialPort1_DataReceived具体如何实现?
- WINFORM的正则怎么写?
- 請問在C#中static關鍵字起什麼作用?
- 请教高手,怎样用网络在客户端和服务器端传送DataTable或DataSet
- 到底有没有System.Data.SQL
- C#怎么获取链接的文本内容
- 除了dotnetfuscator外,还有没有别的软件能对.NET软件模糊化?
- C#于DIRECTX9的连接 特急!!!
this.button1.Click += new System.EventHandler(this.button1_Click);
private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("按钮被按下");
}
//
namespace EventSample
{
using System;
using System.ComponentModel;
// Class that contains the data for
// the alarm event. Derives from System.EventArgs.
//
public class AlarmEventArgs : EventArgs
{
private readonly bool snoozePressed ;
private readonly int nrings;
//Constructor.
//
public AlarmEventArgs(bool snoozePressed, int nrings)
{
this.snoozePressed = snoozePressed;
this.nrings = nrings;
}
// The NumRings property returns the number of rings
// that the alarm clock has sounded when the alarm event
// is generated.
//
public int NumRings
{
get { return nrings;}
}
// The SnoozePressed property indicates whether the snooze
// button is pressed on the alarm when the alarm event is generated.
//
public bool SnoozePressed
{
get {return snoozePressed;}
}
// The AlarmText property that contains the wake-up message.
//
public string AlarmText
{
get
{
if (snoozePressed)
{
return ("Wake Up!!! Snooze time is over.");
}
else
{
return ("Wake Up!");
}
}
}
}
// Delegate declaration.
//
public delegate void AlarmEventHandler(object sender, AlarmEventArgs e);
// The Alarm class that raises the alarm event.
//
public class AlarmClock
{
private bool snoozePressed = false;
private int nrings = 0;
private bool stop = false;
// The Stop property indicates whether the
// alarm should be turned off.
//
public bool Stop
{
get {return stop;}
set {stop = value;}
}
// The SnoozePressed property indicates whether the snooze
// button is pressed on the alarm when the alarm event is generated.
//
public bool SnoozePressed
{
get {return snoozePressed;}
set {snoozePressed = value;}
}
// The event member that is of type AlarmEventHandler.
//
public event AlarmEventHandler Alarm; // The protected OnAlarm method raises the event by invoking
// the delegates. The sender is always this, the current instance
// of the class.
//
protected virtual void OnAlarm(AlarmEventArgs e)
{
AlarmEventHandler handler = Alarm;
if (handler != null)
{
// Invokes the delegates.
handler(this, e);
}
}
// This alarm clock does not have
// a user interface.
// To simulate the alarm mechanism it has a loop
// that raises the alarm event at every iteration
// with a time delay of 300 milliseconds,
// if snooze is not pressed. If snooze is pressed,
// the time delay is 1000 milliseconds.
//
public void Start()
{
for (;;)
{
nrings++;
if (stop)
{
break;
}
else if (snoozePressed)
{
System.Threading.Thread.Sleep(1000);
{
AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
nrings);
OnAlarm(e);
}
}
else
{
System.Threading.Thread.Sleep(300);
AlarmEventArgs e = new AlarmEventArgs(snoozePressed,
nrings);
OnAlarm(e);
}
}
}
}
// The WakeMeUp class has a method AlarmRang that handles the
// alarm event.
//
public class WakeMeUp
{
public void AlarmRang(object sender, AlarmEventArgs e)
{
Console.WriteLine(e.AlarmText +"\n");
if (!(e.SnoozePressed))
{
if (e.NumRings % 10 == 0)
{
Console.WriteLine(" Let alarm ring? Enter Y");
Console.WriteLine(" Press Snooze? Enter N");
Console.WriteLine(" Stop Alarm? Enter Q");
String input = Console.ReadLine();
if (input.Equals("Y") ||input.Equals("y")) return;
else if (input.Equals("N") || input.Equals("n"))
{
((AlarmClock)sender).SnoozePressed = true;
return;
}
else
{
((AlarmClock)sender).Stop = true;
return;
}
}
}
else
{
Console.WriteLine(" Let alarm ring? Enter Y");
Console.WriteLine(" Stop Alarm? Enter Q");
String input = Console.ReadLine();
if (input.Equals("Y") || input.Equals("y")) return;
else
{
((AlarmClock)sender).Stop = true;
return;
}
}
}
}
// The driver class that hooks up the event handling method of
// WakeMeUp to the alarm event of an Alarm object using a delegate.
// In a forms-based application, the driver class is the
// form.
//
public class AlarmDriver
{
public static void Main (string[] args)
{
// Instantiates the event receiver.
WakeMeUp w= new WakeMeUp();
// Instantiates the event source.
AlarmClock clock = new AlarmClock(); // Wires the AlarmRang method to the Alarm event.
clock.Alarm += new AlarmEventHandler(w.AlarmRang); clock.Start();
}
}
}
定义委托
public delegate void AccreditEventHandler(object sender, AccreditEventArgs e);如果参数不是默认的参数,当然还需要定义参数类型如AccreditEventArgs ,以便传送你需要的数据
第二步:
定义事件,类型就是委托类型,前面加event关键字/// <summary>
/// 在选定或取消授权项目及操作时发生。
/// </summary>
public event AccreditEventHandler AccreditAfterCheck;
第三步:
触发事件
//树(授权项) 选中/取消
private void tvwAccreditItems_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
//引发事件
if (AccreditAfterCheck != null)
{
AccreditEventArgs args = new AccreditEventArgs(AccreditActionType.AccreditItem,e.Node.Text,e.Node.Tag,e.Node.Checked); AccreditAfterCheck(this,args);
}
}