【问题标题】:Visual C# Exception Handling(input only numbers and ".") [duplicate]Visual C# 异常处理(仅输入数字和“。”)[重复]
【发布时间】:2013-11-29 09:16:06
【问题描述】:

我目前正在开发我的 Windows 控制台应用程序。有没有办法让用户在TextChanged部分内的文本框中只输入数字和一个点?

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

您的帮助将不胜感激。

【问题讨论】:

标签: c# windows visual-studio


【解决方案1】:

这是一种方法(在 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);
  }

【讨论】:

  • 如何照顾当前的文化?在德国,例如小数点分隔符是 , 而不是 .
  • 另外,Int32 听起来不像解析 十进制 数字的方法。
  • @Uwe Keim,你是对的。我不确定文化是否是问题的一部分。我假设当前的文化将是默认的。我想有人可能会使用 System.Globalization 将特定文化设置为应用程序中的全局设置。
猜你喜欢
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 2021-12-08
  • 2011-04-27
  • 1970-01-01
相关资源
最近更新 更多