Whenever I change the focus from one textbox to another it plays an irritating warning/error beep.
Example:
public void textBox1_KeyPress(object sender, KeyPressEventArgs开发者_运维知识库 e)
{
if (e.KeyChar == (char)Keys.Return)
textBox2.Focus();
}
whenever I press Enter it changes the focus to textBox2 and gives the warning beep.
Any help to disable this would be appreciated. Thank you.
I think you want to add e.Handled = true
to the event handler:
public void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
textBox2.Focus();
e.Handled = true;
}
}
A side node: you should be able to use the KeyCode
instead of the KeyChar
property, avoiding the cast:
public void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
textBox2.Focus();
e.Handled = true;
}
}
e.SuppressKeyPress = true;
精彩评论