Why is my validating event not firing in C#?

[亡魂溺海] 提交于 2019-12-21 03:19:23

问题


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.

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.


回答1:


I've had this before. In your form:

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

ValidateChildren on MSDN



来源:https://stackoverflow.com/questions/558621/why-is-my-validating-event-not-firing-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!