The most crude down and dirty way of doing it is doing something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar);
}
}
You're still not safe with this approach, as users can still copy and paste non-numeric characters into the textbox. You still need to validate your data regardless.