系统在keyDown上发出蜂鸣声 [英] System beep on keyDown

查看:89
本文介绍了系统在keyDown上发出蜂鸣声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。为什么这个代码在点击按钮时发出蜂鸣声?

  void  SearchFeild_KeyDown( object  sender,KeyEventArgs e)
{

// 当用户填写搜索字段并按Enter键时,将点击curent按钮搜索
if (e.KeyCode == Keys.Enter& & btnBack.Visible == false
{
btnSearcch.PerformClick();
e.Handled = true ;
}
else if (e.KeyCode == Keys.Enter& & btnBack.Visible == true
{
btnBack.PerformClick();
e.Handled = true ;
}

}



提前感谢。

解决方案

根据您的描述,SearchFeild是一个控件,它是或包含一个TextBox用于输入。如果未设置多行输入,则输入Enter键时TextBox将发出蜂鸣声。



e.Handled =是的; 声明没有按照你的想法行事。 文档 [< a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.handled%28v=vs.110%29.aspxtarget =_ blanktitle =新窗口> ^ ]这是令人困惑的,人们可以用你的方式来解释它。但是,你需要意识到他们正在谈论在KeyPress事件中设置Handled。更糟糕的是,KeyPressed事件使用KeyPressEventArgs而不是KeyEventArgs。简而言之,文档很糟糕!



而是使用 e.SuppressKeyPress = true;


注意:根据Mit62提供的测试信息,此回复是修订。然而,给出的代码示例是在KeyDown EventHandler中使用'SuppressKeyPress的完全可用的替代方法,如上面的TnTinMan所示。



TnTinMn在正确的轨道上:beep的原因是当设置'AcceptsReturn和'MultiLine属性的某些组合时,.NET WinForms在TextBox中处理Enter KeyPress的方式的工件。



以下是使用KeyPress EventHandler停止beep:

  private   void  SearchFeild_KeyPress( object  sender,KeyPressEventArgs e)
{
if (e.KeyChar == EnterChar)
{
if (btnBack.Visible)
{
btnBack.PerformClick();
}
else
{
btnSearcch.PerformClick();
}

e.Handled = true ;
}
}

请注意,如果您执行类似调用'MessageBox.Show(some text)的操作;在从上面的代码调用的代码中:你将得到一个嘟嘟!


这段代码不会发出任何哔声,我可以向你保证。它在别的地方。您没有显示任何与哔哔声相关的代码。此外,您没有显示与该问题相关的任何代码。这是因为您要求单击该按钮,但显示一些方法可用作您添加到某些控件的某些键盘事件的事件处理程序。此外,使用 PerformClick 并不是最好的主意。通常情况下,当您希望获得与单击按钮(或某物)时相同的效果时,非常简单,将其作为单独的方法。它是这样的:

  void  BackHandler(){ //  无需使用那些不相关的发件人或事件参数 
// ...
}

// ...

myBackButton.Click + =(sender,eventArgs)= > {BackHandler (); };

void SomewhereElse( / * ... * / ){ // 或在其他一些事件处理程序中,无所谓。 ..
// ...
BackHandler() ;
// ...
}

I希望这个想法很清楚。



如果认为你点击不存在的东西,操作系统就会发出哔哔声。要检查它,您可以将声音方案更改为无声音(在控制面板中),看看会发生什么。



-SA

hi all. why this code make beep when buttons clicked?

void SearchFeild_KeyDown(object sender, KeyEventArgs e)
       {

           //when user fill search fields and press Enter the curent button search will be clicked
           if (e.KeyCode == Keys.Enter && btnBack.Visible == false)
           {
               btnSearcch.PerformClick();
               e.Handled = true;
           }
           else if (e.KeyCode == Keys.Enter && btnBack.Visible == true)
           {
               btnBack.PerformClick();
               e.Handled = true;
           }

       }


thanks in advance.

解决方案

Based on you description, "SearchFeild" is some control that is or incorporates a TextBox for input. The TextBox will generate a beep when you input the Enter key if it is not set up for multiline input.

The "e.Handled = true;" statement is not doing what you think here. The documentation [^]for this is confusing, and one could interpret it the way you have. However, you need to realize that they are talking about setting "Handled" in the KeyPress Event. To make matters worse, the KeyPressed event uses KeyPressEventArgs not KeyEventArgs. In short the documentation stinks!

Instead use e.SuppressKeyPress = true;


Note: this reply is revised based on testing information provided by Mit62. However the code example given is a completely usable alternative to using 'SuppressKeyPress in a KeyDown EventHandler as shown by TnTinMan above.

TnTinMn is on the right track: the cause of the "beep" is an artifact of the way that .NET WinForms handles an Enter KeyPress in a TextBox when certain combinations of the 'AcceptsReturn and 'MultiLine properties are set.

Here's an example of using the KeyPress EventHandler to stop the "beep:"

private void SearchFeild_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == EnterChar)
    {
        if (btnBack.Visible)
        {
            btnBack.PerformClick();
        }
        else
        {
            btnSearcch.PerformClick();
        }

        e.Handled = true;
    }
}

Note that if you do something like call 'MessageBox.Show("some text"); in code called from the above code: you are going to get a "beep" !


This code does not perform any beep, I can assure you. It's somewhere else. You are not showing any code related to the beep. Moreover, you are not showing any code related to the problem. This is because you are asking about clicking on the button, but showing some method which could be used as an event handler you added to some keyboard event of some control. Also, using PerformClick is not the best idea. Normally, when you want to have same effect as you would have on the click on a button (or something), quite simply, make it a separate method. It would like this:

void BackHandler() { // no need to use those irrelevant sender or event args
     //...
}

//...

myBackButton.Click += (sender, eventArgs) => { BackHandler(); };

void SomewhereElse(/* ... */) { // or in some other event handler, does not matter where...
    //...
    BackHandler();
    //...
}

I hope the idea is clear.

OS can beep it if "thinks" you are clicking something non-existing. To check it up, you can change sound scheme to "No sound" (in Control Panel) and see what happens.

—SA


这篇关于系统在keyDown上发出蜂鸣声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆