I want user to enter only numeric values in TextBox
.
I got this code:
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
{
int isNumber = 0;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
}
But I am not getting textbox_KeyPress
event and e.KeyChar
while using WPF.
Whats the solution in WPF?
Edit:
I made a Solution!
private void txtName_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
CheckIsNumeric(e);
}
private void CheckIsNumeric(TextCompositionEventArgs e)
{
int result;
if(!(int.TryParse(e.Text, out result) || e.Text == "."))
{
e.Handled = true;
}
}