i want to make a TextBox numeric. The Idea is to check the pressed Key before the Text is shown in the TextBox. I implement the Method KeyDown() of the TextBox.
Here is my Code:
private void txtX_KeyDown(object sender, KeyEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
{
e.Handled = true;
}
}
KeyEventArgs has no KeyChar (I got this from a tutorial)
I use WPF
For windows form
e.SuppressKeyPress = !(e.KeyValue >= 48 && e.KeyValue <= 57);For wpf
e.Handled = !("D1D2D3D4D5D6D7D8D9D0".Contains(e.Key.ToString()));Im using VS2010
Using the same logic U can do a better solution, more elegant 🙂