views:

99

answers:

2

I've got a custom extension of the WinForms DateTimePicker and it works fine, except for the fact that it gives this anoying beep sound whenever I press the escape or enter key.

I tried overriding OnKeyPress like described here, but this way it blocks all of the pressed keys and thus makes it impossible to enter a date or a time using the keyboard. Should I override WndProc or is there another way to solve this problem?

By the way, does anyone know why the beep would be desirable in the first place?

+1  A: 

If you override onkeypress you have to pass through and call the base for the keys you want to accept, and not call the base for enter and escape if you want those ignored. Maybe you should post your code to see if there's a typo in the logic.

The beep normally indicates to the user that the key press did nothing.

AaronLS
I see, thanks for reacting.
Leon
+1  A: 

Just suppress it with a proper KeyDown event handler:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      dateTimePicker1.KeyDown += squelchBeep;
    }
    private void squelchBeep(object sender, KeyEventArgs e) {
      if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Escape) e.SuppressKeyPress = true;
    }
  }
Hans Passant
This did the trick, thanks!
Leon