william007 william007 - 3 months ago 8
C# Question

Multiple Event Handler for Form Control Via VS interface

I have a list box and I want to associate it with two event handler, by the interface in VS 2010 only allows me to specify one as shown below
enter image description here

I guess it might not be a good practise to temper with the designer code, is there anyway to done the adding the eventhandler via Visual studio interface? Thanks.

Answer

is there anyway to done the adding the eventhandler via Visual studio interface

You can always add an event handler in the form's constructor, after the call to InitializeComponent(), ie:

public Form1()
{
     InitializeComponent();
     lstDataset.SelectedIndexChanged += this.SecondEventHandler;
}

That being said, this is likely not a good idea. There isn't really a reason for the same class to subscribe to the same event on the same object more than once. You can just put the code for "both handlers" in the same method.