473,320 Members | 1,958 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,320 software developers and data experts.

Get one event handler to fire for three different text boxes

Hi Gang,

I have written a product registration screen that has three text
boxes. They take 4 numbers each (I know that's too few, but I'm not
the originator of this code). In any event I want them to only allow
numbers as inputs. I would like to have the same event fire when
pressing keys in each of the text boxes:

this.txtCode1.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
this.txtCode2.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
this.txtCode3.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
private void txtCode_KeyPress(object sender, KeyEventArgs e)
{
//if the user typed in a number then...
if (e.KeyValue 47 && e.KeyValue < 58)
{
e.Handled = true;
}

}

I get the same error message 3 times (one for each textbox):
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'

I suspect I'm just missing a keyword somewhere?

Thanks,
Christian
Jul 14 '08 #1
3 3488
Christian Blackburn wrote:
I have written a product registration screen that has three text
boxes. They take 4 numbers each (I know that's too few, but I'm not
the originator of this code). In any event I want them to only allow
numbers as inputs. I would like to have the same event fire when
pressing keys in each of the text boxes:

this.txtCode1.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
this.txtCode2.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
this.txtCode3.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
private void txtCode_KeyPress(object sender, KeyEventArgs e)
{
//if the user typed in a number then...
if (e.KeyValue 47 && e.KeyValue < 58)
{
e.Handled = true;
}

}

I get the same error message 3 times (one for each textbox):
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'

I suspect I'm just missing a keyword somewhere?
No, it's even simpler than that. The second argument of KeyPressEventHandler
is a KeyPressEventArgs, not a KeyEventArgs.

And since C# 2.0, it's no longer necessary to explicitly specify the
delegate type:

this.txtCode1.KeyPress += txtCode_KeyPress;

works just as well (after correcting the declaration).

--
J.
Jul 14 '08 #2
On Jul 15, 1:15*am, Jeroen Mostert <jmost...@xs4all.nlwrote:
Christian Blackburn wrote:
I have written a product registration screen that has three text
boxes. *They take 4 numbers each (I know that's too few, but I'm not
the originator of this code). *In any event I want them to only allow
numbers as inputs. *I would like to have the same event fire when
pressing keys in each of the text boxes:
this.txtCode1.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
this.txtCode2.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
this.txtCode3.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCode_ KeyPress);
* * * * private void txtCode_KeyPress(object sender, KeyEventArgs e)
* * * * {
* * * * * * //if the user typed in a number then...
* * * * * * if (e.KeyValue 47 && e.KeyValue < 58)
* * * * * * {
* * * * * * * * e.Handled = true;
* * * * * * }
* * * * }
I get the same error message 3 times (one for each textbox):
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'
No overload for 'txtCode_KeyPress' matches delegate
'System.Windows.Forms.KeyPressEventHandler'
I suspect I'm just missing a keyword somewhere?

No, it's even simpler than that. The second argument of KeyPressEventHandler
is a KeyPressEventArgs, not a KeyEventArgs.

And since C# 2.0, it's no longer necessary to explicitly specify the
delegate type:

* *this.txtCode1.KeyPress += txtCode_KeyPress;

works just as well (after correcting the declaration).
I don't think you even have to correct the declaration if you use the
short form, due to delegate parameter type contravariance.
Jul 15 '08 #3
Pavel Minaev wrote:
On Jul 15, 1:15 am, Jeroen Mostert <jmost...@xs4all.nlwrote:
>Christian Blackburn wrote:
>>this.txtCode1.KeyPress += new
System.Windows.Forms.KeyPressEventHandler(txtCod e_KeyPress);
<snip>
>> private void txtCode_KeyPress(object sender, KeyEventArgs e)
<snip>
>The second argument of KeyPressEventHandler is a KeyPressEventArgs, not
a KeyEventArgs.

And since C# 2.0, it's no longer necessary to explicitly specify the
delegate type:

this.txtCode1.KeyPress += txtCode_KeyPress;

works just as well (after correcting the declaration).

I don't think you even have to correct the declaration if you use the
short form, due to delegate parameter type contravariance.
KeyPressEventArgs and KeyEventArgs are not in an inheritance relationship
with each other, so you can't use contravariance. You could declare a
delegate that takes EventArgs, but that wouldn't be very useful.

--
J.
Jul 15 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
6
by: Joseph Geretz | last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm adding matching pairs of Toolbar buttons and Menu items. All of the buttons and menu items are wired up to send events to...
9
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the...
19
by: Daniela Roman | last post by:
Hello, I try to fire an event under a button click event and maybe anybody can give a clue please. I have let's say a WEB grid with PageIndexChanged event: private void...
17
by: Eric | last post by:
I'm new to JavaScript and I wrote this code to play with. Oddly, if I enter text in a box and then press the button, I only get the onChange event for the text box and not the button's onclick...
6
by: tbrown | last post by:
I have a combobox with items like this: {one,two,three}. The selected index is 0, so "one" appears in the combobox text. When the user drops down the list, and selects "two", for example, I...
6
by: kirk | last post by:
I have three events, using event handler methods as depicted below. Two of those event handler methods need to reset specific data whenever the other event left fires. I wasn't sure how to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.