Wednesday, October 6, 2010

C# Tutorial: Suppress a Key on a TextBox [Cancel KeyDown event]

Here is the code

using System.Media;
/////////////////////////////////
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if ((e.KeyValue < 48 || e.KeyValue > 57) && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Back)
    {
        e.SuppressKeyPress = true;
        SystemSounds.Beep.Play();
    }
}

Ascii Table

No comments:

Post a Comment