C# 文本框的输入验证:float
这个看似简单的任务却让我有些头疼。 我只是想让用户将 float.TryParse
后面的任何文本输入到 Textboxish 控件中。
我可以使用普通的文本框并检查某些 btnOK_Click 中的文本,但这显然很蹩脚。 另外,还有一个很好的内置 MaskedTextBox 控件,但我未能将其掩码设置为等于 float.TryParse
。 此外,它似乎仅在焦点发生变化时才检查有效性。
在网上挖掘带来了一些有趣的想法,但没有一个像我想要的那样好。
你是如何解决这个问题的? 我是否只是错过了一个明显的解决方案,或者我是否必须自己实现此功能?
我知道 SO 上有一些类似的线程,但没有找到可行的解决方案。
更新:是的,WinForms。
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.
I could use a normal TextBox and check the Text in some btnOK_Click, but this is obviously lame. Also, there is a nice built-in MaskedTextBox control, but I failed to set it's mask to be equal to 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.
Update: Yes, WinForms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑
嗯,这使得它变得更容易......只需添加一个
验证
事件处理程序到您的文本框并在后面的代码中执行
TryParse
。 如果无效,则提示用户。当用户完成输入并将焦点从 TextBox 移开时,将触发验证,因此如果您需要进行即时检查,您可以处理 TextChanged 或 KeyPress/KeyUp 事件处理程序,而不是
Original
是在 asp.net 或 winforms/wpf 中
如果是 asp.net,您可以使用
RegularExpressionValidator
的组合(以考虑逗号分隔、1 个小数点等...)和RangeValidator
设置浮点数的最小/最大值。除此之外,保证它的唯一方法是将文本框包装在 updatepanel 中,在其上粘贴 CustomServerValidator,然后在服务器验证函数中,对
TextBox 执行
值,如果成功,则有效,如果失败,则无效TryParse
。 TextEdit
Well that makes it alot easier... Just add a
Validating
Event Handler to your textboxand do the
TryParse
in the code behind. 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
Original
Is this in asp.net or winforms/wpf
If its asp.net, you could use a combination of
RegularExpressionValidator
(to account for comma seperation, 1 decimal point, etc...) and aRangeValidator
to set the min/max values for a float.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 theTextBox.Text
value, if it succeeds, IS VALID, if it fails, NOT VALID使用
Validating
和验证为 false 时要小心。 您可能会发现,除非您输入有效的数据,否则您无法将焦点从文本框上移开,这是一个非常大的可用性难题。我通过简单地在
LostFocus
上尝试TryParse()
来解决这个问题,如果 TryParse 失败,我将文本框背景着色为淡红色,以明显表明出现问题。Be careful using
Validating
and validating to 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()
onLostFocus
and if the TryParse fails I color the textbox background a reddish tint to make it obvious that something is wrong.