Reputation: 134
I have two buttons. When I start my api and hit Enter then api doing code from buuton1. How to block it?
private void frm2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
}...
Doesn't work.
Upvotes: 0
Views: 1484
Reputation: 5747
You can use the KeyPressEventargs.Handled property for this.
private void frm2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
e.Handled = true;
}
}
For more information, see: https://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.handled(v=vs.110).aspx
Upvotes: 0
Reputation: 21766
You can block a key by setting SuppresKeyPress to true
in your event handler frm2_KeyPress
:
if (e.KeyCode == Keys.Enter) {
e.SuppressKeyPress = true;
}
Please note I have changed e.KeyChar
to e.KeyCode
as personally I find e.KeyCode == Keys.Enter
more readible than (e.KeyChar == (char)13)
Upvotes: 1