关闭控制绕过验证 .NET
所以,我有一个用户控件,它只是一个具有特殊格式规则的文本框(有原因为什么我不只是使用 MaskedTextBox...你知道...明智的解决方案)。
无论如何,当用户输入无效数据时,他们无法使用鼠标单击关闭,因为 TextBox_Validating 事件会按其应有的方式取消,并且在输入可接受的输入之前控件不会失去焦点。但是,如果他们尝试关闭该控件,则会命中“e.Cancel = True”语句(我通过使用断点知道这一点),但 WinForms 仍然允许他们关闭其他控件,从而在文本框。
我缺少什么才能使选项卡正常运行?
So, I have a User Control which is just a TextBox with special formatting rules (there are reasons why I'm not just using a MaskedTextBox...y'know...the sensible solution).
Anyhow, when the user inputs invalid data, they can't click off using the mouse because the TextBox_Validating event cancels like it should and the control can't lose focus until they enter acceptable input. However, if they try to tab off the control, the 'e.Cancel = True' statements are being hit (I know this by using breakpoints) but WinForms is still allowing them to tab off to the other controls, leaving invalid data in the TextBox.
What am I missing to make tabbing act appropriately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 .Net Framework 2.0 版开始,您可以使用
Form.AutoValidate
属性来修改验证行为。有关详细信息,请参阅文档 。我刚刚测试了选项卡问题,当验证失败时(e.Cancel = true),它不允许我离开。当验证成功(e.Cancel = false)时,一切正常。
From version 2.0 of the .Net Framework you can use the
Form.AutoValidate
property to modify the validation behaviour. See the documentation for more information.I just tested the tabbing issue, and it does not alow me to tab away when the validation fails (e.Cancel = true). When the validation succeeds (e.Cancel = false) everyting is alright.