为什么我的验证事件没有在 C# 中触发? [英] Why is my validating event not firing in C#?

查看:34
本文介绍了为什么我的验证事件没有在 C# 中触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态创建的表单.这是一个 winForms 应用程序.

I have a form that is dynamically created. It is a winForms application.

这个表单只是一个菜单和一系列文本框和标签.对于本示例,您可以忽略标签.

This form is just a menu and a series of textboxes and labels. For the sake of this example, you can ignore the labels.

我的问题是:当我在文本框中编辑内容时,然后单击菜单发出保存",最后一个文本框中的文本仍未发出其验证"方法.这似乎是因为控件仍然具有焦点,并且菜单位于单独的线程中.

My problem is: When I edit stuff in the textboxes, then click the menu to issue "Save", the text from the last text box still hasn't issued its "Validating" method. This appears to be because the control still has focus, and that the menu is in a separate thread.

如何在用户单击保存"时强制触发验证事件?我不知道用户在哪个文本框(如果有),并且在保存"函数中发出 SomeKnownControl.Focus(); 似乎没有帮助.

How can I force the validating events to fire when the user clicks "save"? I don't know which text box the user is on (if any), and issuing SomeKnownControl.Focus(); within the "save" function doesn't seem to help.

推荐答案

我以前遇到过这种情况.在您的表单中:

I've had this before. In your form:

private void SaveButtonClick(...)
{
    if (this.ValidateChildren())
    {
        // do save
    }
}

MSDN 上的ValidateChildren

这篇关于为什么我的验证事件没有在 C# 中触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆