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 7 4846
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 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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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.
...
|
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...
|
by: chopsnsauce |
last post by:
Here's the example:
Dim frm As New FORM1
Try
frm.show
Catch ex As Exception
msgbox ex.message
|
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...
|
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. ...
|
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'...
|
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"
...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |