Wednesday, July 20, 2011

TextBox Control to accpet numeric values


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

How to upload app to macOS

1. Open Terminal Press Cmd (⌘) + Space , type Terminal , and hit Enter . 2. Navigate to Your Build Output Directory Your .app file is likel...