文本框的 C# 输入验证:浮动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/828546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:16:30  来源:igfitidea点击:

C# Input validation for a Textbox: float

c#textboxfloating-pointvalidationmaskedtextbox

提问by mafu

This supposedly easy task gave me some headache. I simply want to let the user enter any text that succeeds float.TryParseinto 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. 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.

我可以使用普通的 TextBox 并在一些 btnOK_Click 中检查文本,但这显然是蹩脚的。此外,还有一个不错的内置 MaskedTextBox 控件,但我未能将其掩码设置为等于float.TryParse. 此外,它似乎只在发生焦点变化时检查有效性。

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。

采纳答案by Eoin Campbell

Edit

编辑

Well that makes it alot easier... Just add a ValidatingEvent Handler to your textbox and do the TryParsein the code behind. If its invalid, prompt the user as such.

嗯,这让它变得更容易......只需将一个Validating事件处理程序添加到您的文本框并TryParse在后面的代码中执行。如果无效,则提示用户。

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 RangeValidatorto set the min/max values for a float.

如果是 asp.net,您可以使用RegularExpressionValidator(考虑逗号分隔、1 个小数点等)和 a 的组合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 TryParseon the TextBox.Textvalue, if it succeeds, IS VALID, if it fails, NOT VALID

除此之外,保证它的唯一方法是将文本框包装在更新面板中,在其上粘贴 CustomServerValidator,并在服务器验证函数中,TryParseTextBox.Text值执行 a ,如果成功,则为有效,如果失败,无效

回答by rein

Be careful using Validatingand 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.

小心使用Validating和验证为假。您可能会发现,除非您输入有效数据,否则您无法将焦点从文本框上移开,这是一个非常大的可用性问题。

I solve this by simply trying a TryParse()on LostFocusand if the TryParse fails I color the textbox background a reddish tint to make it obvious that something is wrong.

我通过简单地尝试来解决这个TryParse()问题LostFocus,如果 TryParse 失败,我会将文本框背景着色为淡红色,以使其明显出现问题。