开发者

Disable warning/error beep when changing focus

开发者 https://www.devze.com 2022-12-12 20:31 出处:网络
Whenever I change the focus from one textbox to another it plays an irritating warning/error beep. Example:

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

精彩评论

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