There are a couple of ways, but the most reliable is to handle the TextChanged Event and screen the entire text. This prevents pasting digits as well, which the KeyPress Event doesn't.
private void myTextBox_TextChanged(object sender, EventArgs e)
{
Regex r = new Regex(@"\d");
if (r.IsMatch(myTextBox.Text))
{
int i = myTextBox.SelectionStart;
myTextBox.Text = r.Replace(myTextBox.Text, "");
myTextBox.SelectionStart = i;
}
}
(the SelectionStart stuff is to preserve the caret position)