By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,459 Members | 1,472 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,459 IT Pros & Developers. It's quick & easy.

Get one event handler to fire for three different text boxes

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.