Disable Whole Form by Error Provider

不想你离开。 提交于 2019-12-11 23:32:26

问题


In a Windows form I've some controls and a UserControl. I've a ErrorProvider in the UserControl. I want to stop editing all the controls in the Form if there is an error in the userControl. Is there any way to do that?

I am using errorProvider.BindToCustomDataAndErrors(..)


回答1:


  1. Expose a property on the user control to indicate whether it has any errors (such as by iterating the control collection and checking errorProvider.GetError(control)

  2. Check your property and disable whatever you need to

    if (!myUserControl.IsValid) { someContainerControl.Enabled = false; }

  3. If you need to be notified in 'real time', declare an event on the user control IsValidChanged, attach to it and disable your controls when it fires and IsValid is false.




回答2:


You can prevent the user from moving the focus out of the UserControl with the Validating event. For example:

    protected override void OnValidating(CancelEventArgs e) {
        foreach (Control ctl in this.Controls) {
            if (errorProvider1.GetError(ctl) != "") e.Cancel = true;
        }
        base.OnValidating(e);
    }

Using ErrorProvider.GetError() like this is not ideal although it can work. You might want to keep your own list of validation errors.



来源:https://stackoverflow.com/questions/9095427/disable-whole-form-by-error-provider

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