Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
hi all. why this code make beep when buttons clicked?
C#
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.
Posted
Comments
ZurdoDev 30-Jan-14 12:55pm    
Your windows theme says so?
Sergey Alexandrovich Kryukov 30-Jan-14 12:58pm    
A side note: comparison a Boolean with true or false is pointless and clearly indicates that you have no idea what you are comparing. A literal code would be

if (e.KeyCode == Keys.Enter && btnBack.Visible)
//...


—SA
ZurdoDev 31-Jan-14 12:54pm    
That's harsh. It doesn't mean they don't understand, it's just a simple shortcut but nothing wrong with it.
Sergey Alexandrovich Kryukov 31-Jan-14 13:05pm    
Well, I forgot to mention that this is equivalent, but about "harsh"... You see, I still think that such code is not just too long version. I never argue against some longer code. I still think the only reason to write such condition would be lack of understanding of what is compared with what, or how Boolean works.
If it would be bool? property, this OP's code would be perfect, but with bool this is just lack of understanding. Can you see my point?..
—SA

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;
 
Share this answer
 
Comments
BillWoodruff 30-Jan-14 23:47pm    
Setting the e.SuppressKeyPress Property in a KeyDown EventHandlers in a WinForm TextBox will not suppress the beep if 'MultiLine is 'false, independent of the value of 'AcceptsReturn.

I don't have a problem with the documentation, since, in my view, a KeyDown EventHandler is a very different "animal" than a "KeyPress" EventHandler, and the two are designed to offer different functionality.

However, as implied in my response below, I find the various interactions of TextBox settings with whether a beep is played or not, kind of strange.
mit62 31-Jan-14 1:24am    
I set MultiLine=true and it works without SuppressKeyPress = true;
I set MultiLine=false and it works again with SuppressKeyPress = true;

thanks.
BillWoodruff 31-Jan-14 1:45am    
My tests show identical results if you are setting 'SuppressPress to 'true in the KeyDown Event, or not setting it to 'true, but you certainly don't want to allow multi-line entry if your TextBox is only vertically sized so it will contain only one line of Text !

acceptsreturn multiline

false false beep
true false beep
false true no beep
true true no beep

So, I believe you must handle the KeyPress Event in order to avoid the beep.
BillWoodruff 31-Jan-14 1:50am    
If you get different results in testing, please let me know: always time to learn something new :)
mit62 31-Jan-14 2:17am    
AcceptReturn Multiline SuppressPress Result
False False False beep
False False True nobeep
False True False nobeep
False true True nobeep
True False False beep
True False True nobeep
True True False nobeep
True True True nobeep
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:"
C#
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" !
 
Share this answer
 
v3
Comments
TnTinMn 31-Jan-14 14:28pm    
I have +5 this to try to offset that 2 vote and hope that others will as well.
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:
C#
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jan-14 2:05am    
Use the debugger and see if this line with += is reached. And then see what happens in the handler. Debugger, that's all.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900