private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char c = e.KeyChar;
            //c == 8 is Backspace
            //c == 32 is Space
            //c == 13 is Carriage return (Enter)
            // http://www.asciitable.com/ Tabel of ascii characters
            if ((Char.IsDigit(c) || c == 8) || c == 32 || c == 13) return; 
            e.Handled = true; //
        }
6
Contributors
9
Replies
18
Views
4 Years
Discussion Span
Last Post by Michael27
0

For a numbers with decimal point you add c == 46 and for negative numbers use c == 45

2

If I do that, then -384.2834-234.20394 becomes acceptable, and that isn't a number!

And why are you using the magic numbers rather than the Keys enumeration.

Edited by Momerath

Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.