Possible Duplicate:
How can I make my own event in C#?
Sir, I have some queries regarding events...
- I want to write my own event that should be fired when user click 3 times on a button?
- In a client server application a client is connected to server, now if client disconnects then i want it must trigger an event on the server notifing that it is disconnected. I can achieve this using timer or some loop and a different thread. This thread continuously check the connection of the client. That is it. But it is somthing like polling mechanism. I want to write something like interrupt mechanism. I mean i dont want to use timer o开发者_如何学编程r loop. When a client disconnects willingly or unwillingly either from client side or server side my server should notify that client is disconnected. Please dont tell about how to make custom events. Please help... Thanks...
public event EventHandler<EventArgs> Clicked3Times;
protected virtual void OnClicked3Times()
{
var handler = this.Clicked3Times;
if (handler != null)
{
handler(this,EventArgs.Empty);
}
}
Then to invoke..
this.OnClicked3Times();
1 You can implement a hander for an event of single click on a button. So, it will be executed each time the button is clicked. This handler will count the number of clicks and raise another event if there were 3 clicks.
int nClicks;
event EventHandler TrippleClick;
public Form1()
{
InitializeComponent();
this.button1.Click += new System.EventHandler(this.button1_Click);
nClicks = 0;
TrippleClick = new EventHandler(OnTrippleClick);
}
void OnTrippleClick(object sender, EventArgs e)
{
MessageBox.Show("Tripple click");
}
private void button1_Click(object sender, EventArgs e)
{
nClicks++;
if (nClicks == 3)
{
TrippleClick(sender, e);
nClicks = 0;
}
}
here is good example
basically you have to write something like this:
public event EventHandler MyEvent;
void OnMyEvent() {
if ( MyEvent != null )
MyEvent( new object(), new EventArgs() );
}
and when you want to call your event, in your code you call OnMyEvent()
function
精彩评论