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

If you’re using Visual Studio to build your .NET MAUI app on a Mac, locating the IPA (iOS App Package) file can be a bit tricky

  If you’re using   Visual Studio   to build your   .NET MAUI   app on a   Mac , locating the   IPA (iOS App Package)   file can be a bit tr...