【问题标题】:Disable warning/error beep when changing focus更改焦点时禁用警告/错误提示音
【发布时间】:2009-11-26 23:03:12
【问题描述】:

每当我将焦点从一个文本框更改为另一个文本框时,它都会发出令人讨厌的警告/错误哔声。

例子:

public void textBox1_KeyPress(object sender, KeyPressEventArgs e)  
{  
    if (e.KeyChar == (char)Keys.Return)  
        textBox2.Focus();  
}  

每当我按 Enter 时,它都会将焦点更改为 textBox2 并发出警告声。

任何禁用此功能的帮助将不胜感激。 谢谢。

【问题讨论】:

    标签: c#


    【解决方案1】:

    我认为您想将e.Handled = true 添加到事件处理程序中:

    public void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Return)
        {
            textBox2.Focus();
            e.Handled = true;
        }
    }
    

    侧节点:您应该能够使用KeyCode 而不是KeyChar 属性,避免强制转换:

    public void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            textBox2.Focus();
            e.Handled = true;
        }
    }
    

    【讨论】:

      【解决方案2】:

      e.SuppressKeyPress = true;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-24
        • 2011-11-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-06
        • 2016-07-29
        • 1970-01-01
        • 2023-04-04
        相关资源
        最近更新 更多