在文本框中设置浮点数 [英] set floating point numbers in textbox

查看:97
本文介绍了在文本框中设置浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆文本框,我需要输入数字并也接受小数.我对一组控件有什么建议吗?

I have bunch of textbox that i need to make numeric and accepts decimals too. Is there any suggestion on how i can do this for a group of controls?

推荐答案

由于您没有付出太多,这可能会有所帮助: FilteredTextBox [
Since you don''t give too much away this might help: FilteredTextBox[^]


如果让用户通过键入字符来输入有效的数字数据,就会出现问题.您应该基于TextBox(不是UserControl)创建一个单独的自定义控件类,并公开使用数字数据而不是文本数据的属性,例如:
You problem would be a problem if making the user input valid numeric data by typing characters. You should create a separate custom control class based on TextBox (not UserControl) and expose properties working with numeric data instead of text data, for example:
double Value { get; set; }

可以通过double.ToStringdouble.TryParse在内部设置TextBox文本.

另外,为输入数据的有效范围做好准备.

尝试在一处验证控件时,不要犯一个常见的错误.您应该分两个步骤进行操作.首先,您应该从输入中过滤掉不需要的字符.例如,如果您输入的是浮点数,则可以只允许数字和小数点.至于``-''字符,则应允许它取决于有效范围:如果负值在该范围内,则允许它使用工具.

您没有告诉我们TextBox的确切类型(此名称下有几个不同的控件;因此下一次告诉我们完全限定的名称),我只能告诉您应该在事件或覆盖的虚拟方法OnKeyPress,您可以在其中通过事件参数参数取消对键的处理,并防止输入不需要的字符.请您自己了解详细信息,因为您知道您使用的是哪个UI库,而我们不知道.

现在,回到可以避免的错误:用户输入时不要尝试进行任何数据验证,因为这可能会造成极大的混乱.仅在即将使用数据时验证数据(例如:" A pply"按钮).不要尝试通过使用正则表达式或类似方法分析字符串本身来验证字符串是否代表有效的数字格式,这是另一个常见错误.您的验证应包括两个步骤:1)使用string.TryParse;如果成功,则2)验证数值是否在有效的输入范围内.

对于无限制的输入范围或没有下限或上限的范围,请使用该类型的限制.这很有趣.例如,您需要类型为int的无限值.这是微不足道的事情:使用int.MinimumValueint.MaximumValue,或同时使用两者.浮点数dounle怎么样?使用值double.NegativeInfinitydouble.PositiveInfinity.这些值是有效的数值,可以与比较运算符``>'',``<'',``> =''和``< =''正确使用.

—SA

which would provide setting the TextBox text internally via double.ToString and double.TryParse.

Also, make a provisions for valid range of input data.

You should not do a common mistake trying to validate control in one place. You should do it in two steps. First, you should filter out unwanted characters from the input. For example, if your input is floating point, you can allow digits only and decimal point. As to the ''-'' characters, you should allow it depending and the valid range: if negative values are in the range, allow it, tool.

You did not tell us exact type of TextBox (there are several different controls under this name; so next time tell us fully-qualified name), I can only tell you that you should do filtering in the handler of the event KeyPress or overridden virtual method OnKeyPress where you can cancel processing of the key through event arguments parameter and prevent input of unwanted character. Please find out the detail by yourself, as you know what UI library you are using, and we don''t know it.

Now, coming back to the mistake to be avoided: don''t try to do any data validation as the user types as it could be highly confusing. Validate data only when it is about to be used (example: "Apply" button). Don''t try to validate string for representing a valid numeric format by analyzing the string itself using Regular Expressions or anything like that — this is another common mistake. Your validation should have two steps: 1) use string.TryParse; if it was successful, 2) validate the numeric value to be in valid input range.

For unlimited input range or the range without low or high limit, use the limits of the type. This is something interesting. For example, you need unlimited value of the type int. This is trivial thing: use int.MinimumValue or int.MaximumValue, or both. How about floating point, say, dounle? Use the values double.NegativeInfinity or double.PositiveInfinity. These values are valid numeric values and can be correctly used with comparison operators ''>'', ''<'', ''>='' and ''<=''.

—SA


您好,


http://stackoverflow.com/questions/243489/decimal-textbox-in-windows-forms [^ ]
Hello,


http://stackoverflow.com/questions/243489/decimal-textbox-in-windows-forms[^]


这篇关于在文本框中设置浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆