Remove Event Handlers in C#

  • Thread starter Thread starter Prasad Dannani
  • Start date Start date
P

Prasad Dannani

Hi,

I am currently working on C#.

I need to temporarity add remove event handlers
which i was done in VB.NET using Addhandler and RemoveHandler functions.

How to do it in c#

Thanks in Advance
Prasad Dannani
 
Hi

The += and -= are what you are after.

For example, say I have a combo box and I want to add an event handler
to the index changed event. It is as easy as:

cmbFonts.SelectedIndexChanged += new
EventHandler(cmbFonts_SelectedIndexChanged);

Where my event handling routine matches the signature of the
EventHandler delegate. For example:

private void cmbFonts_SelectedIndexChanged(object sender,
System.EventArgs e)
{
// Do some work here
}

When I want to remove the event handler:
cmbFonts.SelectedIndexChanged -= new
EventHandler(cmbFonts_SelectedIndexChanged);

Cheers
Bill
 
Back
Top