开发者

Disable warning/error beep when changing focus

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;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜