如何在验证 NumericUpDown 控件之前访问其文本?
我有一个带有 System.Windows.Forms.NumericUpDown 控件的表单。
假设范围是从 0 到 100,当前值(通过微调器获得)是 100。我可以输入超出允许范围的数字(例如 567),但是当我单击表单上的“确定”时重置该值,它只是默默地将超出范围的值设置为 100 并关闭表单。
客户希望得到明确的消息,表明该号码超出范围。因此,我查看了关闭表单上的 NumericUpDown.Text
属性,但该属性返回的是“100”而不是“567”。
我在哪里(或如何)可以“捕获”控件中出现的文本是“567”的事实?
I have a form with a System.Windows.Forms.NumericUpDown control.
Say that the range is from 0 to 100, and the current value (arrived at via the spinner) is 100. I can type in a number that is outside of the allowable range (say 567) but when I click OK on the form to reset the value, it just silently sets the out of range value to 100 and closes the form.
The customer wants an explicit message that the number is out of range. So, I looked at checking the NumericUpDown.Text
property on the form close, but that property gives me back "100" not "567".
Where (or how) can I "catch" the fact that the text appearing in the control is "567"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 这个问题通过获取对
NumericUpDown
内的TextBox
的引用并处理其来捕获无效值验证
事件。在您的处理程序中,TextBox.Text
属性将具有要测试的无效值。在 .NET 2.0 Winforms 中为我工作。You can use the answer from this question to capture the invalid value by getting a reference to the
TextBox
inside theNumericUpDown
and handling itsValidating
event. In your handler, theTextBox.Text
property will have the invalid value to test against. Works for me in .NET 2.0 Winforms.你可以尝试这个,唯一的是该值仍然会重置为 100,但用户仍然会收到超出范围值的通知:
You could try this, the only thing is that the value will still be reset to 100, but the user will still be notified of the out of range value: