Search code examples
c#.netwinformsdatetimepicker

How to avoid beep sound of DateTimePicker


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?


Solution

  • 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;
        }
      }