0

I have three controls on my form - a texbox, button and a picturebox. After clicking a button, both textbox and button itself become inactive. Also I have and Form1_KeyDown(object sender, KeyEventArgs e) to send pressed buttons that will be displayed on picturebox, but for some reason everytime I press any key it beeps. I've noticed that it happens only with button and TextBox deactivated. Why is that happening and how do I press keys without that annoying beeps?

e.SuppressKeyPress = true is preventing the input of keys to control, I don't need that, I just need to rid of that sound (why is it there anyway, when I'm disabling two controls?)

UPD: I think the problem is that PictureBox doesn't have a focus and also a KeyDown event.

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65

1 Answers1

0

e.SuppressKeyPress = true is preventing the input of keys to control, I don't need that, I just need to rid of that sound

You should set the SuppressKeyPress only if you handled the event manually. So typically for Enter/Escape buttons:

// suppress key only if you handled the keystroke
void txtValue_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.SuppressKeyPress = true;
        if (IsModified)
            SaveData();
    }
    else if (e.KeyCode == Keys.Escape)
    {
        e.SuppressKeyPress = true;
        if (IsModified)
            ResetData();
    }
}
György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • Thanks for answering! Application beeps if I press any key on keyboard, not just Enter or Alt or something. Everty key that I press make this sound. But this happens only when other controls disabled. I can't see how can I use this code to my case. – Александр Пушкин Nov 20 '15 at 14:36
  • Does it mean that you change the focus on each keystroke? In this case maybe you should always suppress the handling. – György Kőszeg Nov 20 '15 at 15:13
  • No, I don't change focus at all. I suggest it's done automatically when other controls are disables. But beep sounds on every keystroke, no matter what I pressed. – Александр Пушкин Nov 20 '15 at 15:18