Following sample shows WPF TextBox which accept only numeric values.
XAML File:
<TextBox Background="#FFF1F8FE" Name="tStudy" PreviewTextInput="TextBox_NymericInput" ></TextBox>
CodeBehind:
private void TextBox_NymericInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = !AreAllValidNumericChars(e.Text);
}
bool AreAllValidNumericChars(string str)
{
bool ret = true;
int l = str.Length;
for (int i = 0; i < l; i++)
{
char ch = str[i];
ret &= Char.IsDigit(ch);
}
return ret;
}
No comments:
Post a Comment