-1

I am currently working on my Windows Console app. Is there any way to let the users only input numbers and one dot in the text box, inside the TextChanged part?

private void Input_TextChanged(object sender, TextChangedEventArgs e)
{
}

Your help would be appreciated.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
wendy
  • 105
  • 1
  • 1
  • 6

1 Answers1

1

This is one way (See more on MSDN TryParse):

  string inValue="123.1"; //for example
  decimal number;
  bool result = decimal.TryParse(inValue, out number);
  if (result)
  {
     //The entered number is valid number (1 decimal)         
  }
  else
  {
     //bad input number.
     if (inValue == null) inValue = ""; 
     Console.WriteLine("Attempted conversion of '{0}' failed.", inValue);
  }
NoChance
  • 5,632
  • 4
  • 31
  • 45
  • How about taking care of the current culture? In Germany, e.g. the decimal separator is a `,` not a `.`. – Uwe Keim Nov 15 '13 at 21:26
  • Plus, `Int32` doesn't sound like the way to parse _decimal_ numbers. – Uwe Keim Nov 15 '13 at 21:27
  • @Uwe Keim, you are correct. I am not sure the culture was part of the question though. I assume the current culture will be the default. I guess one would possibly use System.Globalization to set a specific culture as a global setting in the application. – NoChance Nov 15 '13 at 21:34