-1

I need real numbers in TextBox. I try my code online here

"^-{0,1}[0-9]{1,3},{0,1}[0-9]{1,2}$"

and its working perfectly, but in my project not working. Please show me how I must do it

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Qwert11
  • 15
  • 1
    Please show your code, we don't know why it isn't working without seeing the code. – Mixxiphoid Nov 16 '17 at 09:40
  • you can try the [Gu.Wpf.NumericInput](https://github.com/JohanLarsson/Gu.Wpf.NumericInput) – FoggyFinder Nov 16 '17 at 09:41
  • private void c_PreviewTextInput(object sender, TextCompositionEventArgs e) { Regex regex = new Regex ("^-{0,1}[0-9]{1,3},{0,1}[0-9]{1,2}$"); e.Handled = regex.IsMatch(e.Text); } – Qwert11 Nov 16 '17 at 09:48
  • 1
    https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf – ASh Nov 16 '17 at 09:55

1 Answers1

0

Would TryParse works for you? From MSDN:

Converts the string representation of a number in a specified style and culture-specific format to its double-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.

And for you, it could be something like this:

private void c_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    double num;
    e.Handled = double.TryParse(e.Text, out num);
    // if e.Text is a number, e.Handled will be true and num = e.Text
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105