c# - 更改焦点时禁用警告/错误蜂鸣

标签 c#

每当我将焦点从一个文本框更改到另一个文本框时,它就会发出恼人的警告/错误蜂鸣声。

例子:

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

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

如能提供任何帮助来禁用此功能,我们将不胜感激。 谢谢。

最佳答案

我认为您想将 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;
    }
}

关于c# - 更改焦点时禁用警告/错误蜂鸣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1806049/

相关文章:

c# - 单元测试 DateTime 字符串

c# - c#中的整数提升(以sbyte的范围为例)

c# - System.Windows.Forms.TextBox 中未显示 Unicode 字符

c# - PdfStamper正在处理输出流吗? (iTextSharp)

c# - 如何将DataGridView动态添加到TabPage

c# - 禁用 "Name can be simplified"IDE0003 修复提示

c# - C# 中允许在 O(1) 中反转的数据结构

c# - Azure blob DownloadToStream 花费的时间太长

c# - 我怎样才能得到我所有显示器的比例因子?

c# - 如何避免ef以上业务逻辑类中的重复代码?