通过错误提供者禁用整个表单

发布于 2025-01-02 04:33:29 字数 177 浏览 1 评论 0原文

在 Windows 窗体中,我有一些控件和一个 UserControl。我在 UserControl 中有一个 ErrorProvider 。如果用户控件中存在错误,我想停止编辑表单中的所有控件。有什么办法可以做到这一点吗?

我正在使用 errorProvider.BindToCustomDataAndErrors(..)

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(..)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤檠 2025-01-09 04:33:29
  1. 在用户控件上公开一个属性,以指示它是否有任何错误(例如通过迭代控件集合并检查 errorProvider.GetError(control)

  2. 检查您的属性并禁用您需要的任何内容

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

  3. “实时”收到通知,请在用户控件 IsValidChanged 上声明一个事件,附加到该事件,并在该事件触发并且 IsValid 为 false 时禁用您的控件。

  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.

场罚期间 2025-01-09 04:33:29

您可以使用 Validating 事件阻止用户将焦点移出 UserControl。例如:

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

像这样使用 ErrorProvider.GetError() 虽然可以工作,但并不理想。您可能想保留自己的验证错误列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文