Tuesday, May 29, 2012

Limiting Keystroke To Only Numbers In Windows Application C#

here is the code



 private void txtNumber_KeyDown(object sender, KeyEventArgs e)
        {
            char key = (char)e.KeyData;
            if (e.KeyData == Keys.Back)
            {
                e.SuppressKeyPress = false;
            }
            else
            {
                if (!char.IsDigit(key) || e.Shift || e.Alt || e.Control)
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

This code will limit the key stroke to only numbers, shift, alt and control.

No comments:

Post a Comment