Sunday 6 June 2010

One-liners: Prevent beep after pressing 'enter' in a control

If you're a fan of your keyboard shortcuts, you might notice that windows sometimes emits a beep (or bell) after pressing enter to select something. Prevent the bell by setting the KeyDown eventargs SuppressKeyPress member to 'true' if the enter key was pressed.

protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true; // stop the bell!!!
}

One-liners: Designer autogenerates Property = null

Sick of the c# designer setting your control's property to null in the InitializeComponent() function? Then simply set the property's default value to null so InitializeComponent() doesn't have to!

[DefaultValue(null)]
My Property...