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;
}
}
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
char c = Convert.ToChar(e.Text);
if (Char.IsNumber(c))
e.Handled = false;
else
e.Handled = true;
base.OnPreviewTextInput(e);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With