1

Possible Duplicate:
How can I make my own event in C#?

Sir, I have some queries regarding events...

  1. I want to write my own event that should be fired when user click 3 times on a button?
  2. 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 or 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...
Community
  • 1
  • 1
himanshu
  • 417
  • 5
  • 18
  • Please dont tell about how to make custom events. I know that very well. Tell me something what i am asking. plzzzzz..... – himanshu Apr 04 '11 at 09:28

3 Answers3

2
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();
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
1

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;
    }
}
fdermishin
  • 3,519
  • 3
  • 24
  • 45
  • Sir I know what u r saying but how to count user clicks without using Click event of .net framework. – himanshu Apr 04 '11 at 09:25
  • And in Client server query When to raise event that client is disconnected. How to justify that client is no longer connected – himanshu Apr 04 '11 at 09:26
  • I think that without Click event it will be much more sophisticated and not portable because it will be nesessary to work with operating system API, IMHO. – fdermishin Apr 04 '11 at 09:32
  • ok.. Then its better to use click event.. – himanshu Apr 04 '11 at 10:08
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

Soul Reaver
  • 2,012
  • 3
  • 37
  • 52