简体   繁体   English

C#输入文本框的验证:float

[英]C# Input validation for a Textbox: float

This supposedly easy task gave me some headache. 这个简单的任务给我带来了一些麻烦。 I simply want to let the user enter any text that succeeds float.TryParse into a Textboxish control. 我只想让用户输入任何成功的float.TryParse文本到Textboxish控件中。

I could use a normal TextBox and check the Text in some btnOK_Click, but this is obviously lame. 我可以使用普通的TextBox并检查一些btnOK_Click中的Text,但这显然很蹩脚。 Also, there is a nice built-in MaskedTextBox control, but I failed to set it's mask to be equal to float.TryParse . 此外,有一个很好的内置MaskedTextBox控件,但我没有设置它的掩码等于float.TryParse Also, it seems to check for validity only when a focus change occurs. 此外,它似乎仅在发生焦点变化时检查有效性。

Digging around on the net brought some interesting ideas, but none of them as nice as I would like. 在网上挖掘带来了一些有趣的想法,但没有一个像我想的那样好。

How did you solve this problem? 你是怎么解决这个问题的? Did I simply miss an obvious solution, or do I have to implement this functionality myself? 我是否只是错过了一个明显的解决方案,还是我必须自己实现这个功能?

I'm aware of a few similar threads on SO, but there was no feasible solution to be found. 我知道SO上有一些类似的线程,但没有找到可行的解决方案。

Update: Yes, WinForms. 更新:是的,WinForms。

Edit 编辑

Well that makes it alot easier... Just add a Validating Event Handler to your textbox and do the TryParse in the code behind. 好吧,这让它变得更容易......只需在您的文本框中添加一个Validating事件处理程序,并在后面的代码中执行TryParse If its invalid, prompt the user as such. 如果它无效,则提示用户。

Validating will fire when the user is finished typing and moves focus from the TextBox so if you need to do on the fly checking, you could handle the TextChanged or on of the KeyPress/KeyUp Event handlers instead 当用户完成输入并从TextBox移动焦点时,将激活验证,因此如果您需要进行快速检查,则可以处理TextChanged或KeyPress / KeyUp事件处理程序。

Original 原版的

Is this in asp.net or winforms/wpf 这是在asp.net或winforms / wpf

If its asp.net, you could use a combination of RegularExpressionValidator (to account for comma seperation, 1 decimal point, etc...) and a RangeValidator to set the min/max values for a float. 如果是asp.net,你可以使用RegularExpressionValidator (用于说明逗号分隔,1小数点等等)和RangeValidator来设置浮点数的最小值/最大值。

Aside from that, the only way to guarantee it would be to wrap the textbox in an updatepanel, stick a CustomServerValidator on it, and in the server validate function, do a TryParse on the TextBox.Text value, if it succeeds, IS VALID, if it fails, NOT VALID 除此之外,保证它的唯一方法是将文本框包装在updatepanel中,在其上粘贴CustomServerValidator,并在服务器验证函数中,对TextBox.Text值执行TryParse ,如果成功,则为有效,如果失败,则无效

Be careful using Validating and validating to false. 使用Validating和验证为false时要小心。 You might find that, unless you enter valid data, you can't move focus off the textbox which is a really big usability pain. 您可能会发现,除非您输入有效数据,否则您无法将焦点从文本框移开,这是一个非常大的可用性痛苦。

I solve this by simply trying a TryParse() on LostFocus and if the TryParse fails I color the textbox background a reddish tint to make it obvious that something is wrong. 我通过在LostFocus上尝试TryParse()来解决这个问题,如果TryParse失败,我会将文本框背景的颜色设置为偏红色,以明显出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM