views:

167

answers:

5

VS2008, C#, .NET FRAMEWORK2.0

I want this: click button1, webbrowser1._DocumentCompleted() event revokes doA(); click button2, it revokes doB(); click button3, it revokes doC().

I know how to do it using JAVA and I guess C# has this mechanism too. Could anyone give me some idea or better, show me some example?

+2  A: 
myButton.Click += myButton_Click;

protected void myButton_Click(object sender, EventArgs e) {}
hunter
A: 

If you want to add event handlers to a control, which is what I think you are describing, you can easily do this. One common approach is to assign control event handlers in the code behind during page load:

protected void Page_Load(object sender, EventArgs e)
{
    //add the event handler for the click event. You could provide other
    //logic to determine dynamically if the handler should be added, etc.
    btnButton1.Click += new EventHandler(btnButton1_Click);
    btnButton2.Click += new EventHandler(btnButton2_Click);
    btnButton3.Click += new EventHandler(btnButton3_Click);
}

protected void btnButton1_Click(object sender, EventArgs e)
{
    //get the button, if you need to...
    Button btnButton1 = (Button)sender;

    //do some stuff...
    DoA();
}

protected void btnButton2_Click(object sender, EventArgs e)
{    
    //do some stuff...
    DoB();
}

protected void btnButton3_Click(object sender, EventArgs e)
{    
    //do some stuff...
    DoC();
}



private void DoA() {}
private void DoB() {}
private void DoC() {}
KP
+1  A: 

To Add a handler

button.Click += buttonClickEventHandler;

To remove a handler

button.Click -= buttonClickEventHandler;

Rob Goodwin
A: 

To add to these answers, you can also add an anonymous method to an event:

myButton.Click += (object sender, EventArgs e) => {
    MessageBox.Show("MASSIVE ERROR!");
};

What this means is that you can effectively call a method even if it does not match the appropriate event handler signature:

myButton.Click += (object sender, EventArgs e) => {
    DoA();
};

Or (without using a lamba expression):

myButton.Click += delegate(object sender, EventArgs e) {
    DoA();
};
Dan Tao
I guess this won't work with .Net 2.0..just with 3.5+ if I'm not wrong
Juri
@Juri: it's not a function of .NET - anonymous methods are a feature of C# 3.0, which has nothing to do with .NET 3.0 or .NET 3.5. You can use C# 3.0 features in .NET 2.0 programs.
John Saunders
@Juri: If you're using an older version of C#, the above won't compile; but you can still use anonymous methods, just using a different syntax. (See my edit.)
Dan Tao
@Dan: ok, the last version is fine. I was just concerned about the lambda expression, but you're totally right.
Juri
A: 

Declaring an event

public class MyClass1
{
    ...

    public event EventHandler<EventArgs> NotifyValidate;
    protected void RaiseNotifyValidate(EventArgs e)
    {
       if (NotifyValidate != null)
       {
          NotifyValidate(this, e);
       }
    }

    ...
}

Firing that event in your code

...
RaiseNotifyValidate(new EventArgs()); //EventArgs could be more sophisticated, containing data etc..

Registering for that event in your code:

...
MyClass aObj = new MyClass();
aObj.NotifyValidate += new EventHandler(onNotifyValidate);
...
private void onNotifyValidate(object sender, EventArgs e)
{
    //do what you need to
}

As Dan pointed out, with Lambda expressions you can define events like

aObj.NotifyValidate += (s,ev) => 
   {
      //handle your event
   };
Juri