有没有办法识别 .net windows 窗体代码隐藏中最后一个处于活动状态的控件?
我有一个表单,其中有一组无线电控件和一些带有文本框的控件。 如果光标位于文本框上并且我尝试将光标切换到不同的单选按钮,我应该能够识别最后一个活动控件(在本例中......文本框)并进行一些验证。
文本框的LostFocus()事件弹出消息,指示“此项应填写..”。 但是,如果我想在同一组中使用不同的单选按钮选项,我不希望不必要地弹出此消息。
我该如何避免这种情况?
I have a form where there is group of radio controls and some with textbox.
If the cursor is on textbox and i try switching the cursor to a different radio button, i should be able to identify the last active control( in this case.. the textbox) and do some validation.
The LostFocus() event of textbox pops up message indicating that "this item should be filled in..".
But if i want to go with a different radiobutton option in the same group, i dont want this message popping unnecessarily.
How do i avoid that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TextBox 有 Validating 和 Validated 事件——您应该使用它们而不是 LostFocus 事件。在验证的情况下,如果条件不正确,您可以阻止用户离开文本框。如果您必须使用像 LostFocus 这样的“东西”,请改用 Leave 事件。
没有“最后活动控制”类型的功能。您必须通过在这些控件的 Enter 事件上设置变量来自行跟踪。
在我看来,这将导致维持一个丑陋的混乱。在表单级别进行验证可能是最终用户的最佳选择。
The TextBox has Validating and Validated events-- you should use those instead of the LostFocus event. In the case of Validating, you can stop the user from leaving the TextBox if the criteria isn't correct. If you must use "something" like LostFocus, use the Leave event instead.
There is no "Last Active Control" type function. You would have to track that yourself by setting a variable on the Enter event of those controls.
That will lead to an ugly mess to maintain in my opinion. Validate at the form level is probably the best choice for the end user.