Handling multiple events

  • Thread starter Thread starter Java Challenge
  • Start date Start date
J

Java Challenge

Dear all,
I'd like to handle two similar events with the same code (pressing
enter in a search textbox AND clicking on the button "search"). I could
copy the code in both events handlers but this looks like a very
unpleasant solution.

I think that in VB.NET you can do something like:
Sub mybutton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mybutton.Click, mytextbox.KeyPress,
<whaterver event I like>

How do you do that in C#?

I am really a newby so I apologize for the simple question, but
searching google didn't give me any solution.

Thanks in advance. JC
 
mybutton.Click += new EventHandler(mybutton_Click);
mytextbox.KeyPress += new KeyPressEventHandler(mybutton_Click);

IIRC, that will work correctly in C# 2.0 but not 1.1, because
mybutton_Click's parameter types don't exactly match what
KeyPressEventHandler wants.

Another, perhaps easier, way would be to use separate event handlers,
but move the meat of the code into a separate method that you'll call
from both:

public void mybutton_Click(object sender, EventArgs e)
{
PerformSearch();
}

public void mytextbox_KeyPress(object sender, KeyPressEventArgs e)
{
PerformSearch();
}

private void PerformSearch()
{
// do the real work here
}

Jesse
 
Java said:
I'd like to handle two similar events with the same code (pressing
enter in a search textbox AND clicking on the button "search"). I could
copy the code in both events handlers but this looks like a very
unpleasant solution.

I think that in VB.NET you can do something like:
Sub mybutton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mybutton.Click, mytextbox.KeyPress,
<whaterver event I like>
How do you do that in C#?

Well, if they both had the same signature, you could just add the
handler to both events:

mybutton.Click += new EventHandler (mybutton_Click);
mytextbox.KeyPress += new EventHandler (mybutton_Click);

However, KeyPress takes a KeyPressEventHandler. You actually need to
filter for just when the user hits return, I suspect. Either make the
KeyPressEventHandler call mybutton_Click, or create a separate method
(PerformSearch) which both handlers call.

Jon
 
event1 += new EventHandler(methodName);
event2 += new EventHandler(methodName);

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
Jesse said:
(cut) Another, perhaps easier, way would be to use separate event handlers,
but move the meat of the code into a separate method that you'll call
from both:

public void mybutton_Click(object sender, EventArgs e)
{
PerformSearch();
}

public void mytextbox_KeyPress(object sender, KeyPressEventArgs e)
{
PerformSearch();
}

private void PerformSearch()
{
// do the real work here
}

Thank you everybody for your answers.

In particular I thank _Jesse_ for the code example that I will actually
implement, and thank to both _Jesse_ and _Jon_ for explaining me a bit
more about event handling.

I have still a doubt. With the framework 1.1 could I do something like
this?
mybutton.Click += new EventHandler(PerformSearch);
mytextbox.KeyPress += new KeyPressEventHandler(PerformSearch);
Where PerformSearch() is a private void method.
(I won't actually do it because I need to check if the key pressed is
(char)13, but I just wonder from a "theorical" viewpoint, if the
compiler would accept that.)

Thanks. JC
 
Java Challenge wrote:

I have still a doubt. With the framework 1.1 could I do something like
this?
mybutton.Click += new EventHandler(PerformSearch);
mytextbox.KeyPress += new KeyPressEventHandler(PerformSearch);
Where PerformSearch() is a private void method.
(I won't actually do it because I need to check if the key pressed is
(char)13, but I just wonder from a "theorical" viewpoint, if the
compiler would accept that.)

No, you can't. In 1.1, the target method of a delegate must *exactly*
match the signature of the delegate. In 2.0 I believe you could, so
long as PerformSearch matched the EventHandler signature. See
http://msdn2.microsoft.com/en-us/library/ms173174.aspx for why.

Jon
 
Hi,

I have still a doubt. With the framework 1.1 could I do something like
this?
mybutton.Click += new EventHandler(PerformSearch);
mytextbox.KeyPress += new KeyPressEventHandler(PerformSearch);
Where PerformSearch() is a private void method.
(I won't actually do it because I need to check if the key pressed is
(char)13, but I just wonder from a "theorical" viewpoint, if the
compiler would accept that.)


No, you can't cause they have different signatures, you could do it if for
an event with the same signature like mytextbox.GotFocus you can do it.

Take a look at MSDN regarding delegates

cheers,
 
Back
Top