471,319 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Handling multiple events

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

Dec 8 '05 #1
7 4786
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

Dec 8 '05 #2
Java Challenge wrote:
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

Dec 8 '05 #3
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.

"Java Challenge" <ja***********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Dec 8 '05 #4
Jesse McGrew wrote:
(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

Dec 8 '05 #5
Java Challenge wrote:

<snip>
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

Dec 8 '05 #6
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 8 '05 #7
Jon Skeet [C# MVP] wrote:
(cut) 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.


Thank you Jon and Ignacio. You are fantastic guys! :-)

JC

Dec 8 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by scsharma | last post: by
12 posts views Thread by Jack Russell | last post: by
1 post views Thread by pagates | last post: by
4 posts views Thread by reggiestyles | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.