This sets the textbox for only numbers

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)
            // Tabel of ascii characters
            if ((Char.IsDigit(c) || c == 8) || c == 32 || c == 13) return; 
            e.Handled = true; //

What about negative numbers, or numbers with decimal points? And what number has a space in it?

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

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.

I didn't know about Keys enumeration.

Regexpression can be used to accomplish this variety of options.


<input  type="text" onkeyup="this.value=this.value.replace(/[^\d]/,'');">