473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 4863
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric Newton | last post by:
VB's more declarative nature of handling events is golden. I'm hoping C# will acquire this type of deal, in addition to the anonymous delegates. could do same as vb (actually would be easier to...
12
by: scsharma | last post by:
Hi, I am working on creating a webapplication and my design calls for creating main webform which will have menu bar on left hand side and a IFrame which will contain all the forms that are shown...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
2
by: John Dann | last post by:
This question has arisen from an earlier thread but is really a separate issue: I have a VB.Net listbox control on a form. I want to be able to do 2 things with items displayed within the one...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
1
by: pagates | last post by:
Hello All, In C#, one function can handle events from multiple, as long as it has the same delagate signature. For example, there might be a single event that handles all the...
7
by: Jay Douglas | last post by:
Greetings, I have a Windows form application that (naturally) instantiates all sorts of objects. I have a base object that contains an event. Lots of other objects inherit from this event. ...
4
by: reggiestyles | last post by:
Hi, I've got a question about prototype and event handling. I've got several div's (dynamic number) on a page that I want to set as active or inactive (basically, I'm using scriptaculous'...
8
by: sankar.vikram | last post by:
Hi I am using a com component in my C# application. But i getting error while i tried to call multiple events from my application. I'm getting the error "Exception from HRESULT: 0x80040202" ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.