如何在ErrorProvider1上显示标签错误

Ale*_*lex 3 vb.net errorprovider winforms

目标

我想在出现错误时显示我在Label的"Error on ErrorProvider1上的错误"属性中放置的文本.请参阅下面的标签属性.

ErrorProvider1出错

我尝试将红色矩形中的文本显示到我的ErrorProvider1 SetError(control, value)函数中.

 If TextBox1.Text.Trim.Contains("'") Then
    ErrorProvider1.SetError(lblErr, ErrorProvider1.GetError(lblErr))
Else
    ErrorProvider1.SetError(lblErr, "")
End If
Run Code Online (Sandbox Code Playgroud)

如何从lblErr检索"ErrorProvider1上的错误"文本以在ErrorProvider1 SetError值中显示它?

Han*_*ant 7

ErrorProvider组件非常难以有效使用.但它是可以修复的,我将在C#中给出一个示例,它扩展了具有一些新功能的组件:

  • ShowError(Control ctl,bool enable)显示在enable参数为true 时在设计时输入的文本.更容易使用的SetError()版本.
  • 如果显示任何活动警告图标,则HasErrors返回true.在OK按钮的Click事件处理程序中很方便.
  • FocusError()将焦点设置为具有警告图标的第一个控件(如果有).如果没有剩余警告,则返回false.
  • SetError()是ErrorProvider.SetError()的替代品.如果在窗体的Load事件触发后添加任何控件或者需要修改警告文本,则只需要它.

在项目中添加一个新类并粘贴下面显示的代码.编译.将其从工具箱顶部拖放到表单上.设计时行为是相同的.经过适度测试.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.ComponentModel.Design;

class MyErrorProvider : ErrorProvider {
    public void ShowError(Control ctl, bool enable) {
        // Easy to use version of SetError(), uses design-time text
        if (!enable) base.SetError(ctl, "");
        else {
            if (errors.ContainsKey(ctl)) base.SetError(ctl, errors[ctl]);
            else base.SetError(ctl, "No error text available");
        }
    }

    public bool HasErrors {
        // True if any errors are present
        get {
            foreach (var err in errors)
                if (!string.IsNullOrEmpty(base.GetError(err.Key))) return true;
            return false;
        }
    }

    public bool FocusError() {
        // Set the focus to the first control with an active error
        foreach (var err in errors) {
            if (!string.IsNullOrEmpty(base.GetError(err.Key))) {
                err.Key.Focus();
                return true;
            }
        }
        return false;
    }

    public new void SetError(Control ctl, string text) {
        // Use this only to add/modify error text after the form's Load event
        if (!string.IsNullOrEmpty(text)) {
            if (errors.ContainsKey(ctl)) errors[ctl] = text;
            else errors.Add(ctl, text);
        }
        base.SetError(ctl, text);
    }

    private void initialize(object sender, EventArgs e) {
        // Preserve error text
        copyErrors(((Form)sender).Controls);
    }
    private void copyErrors(Control.ControlCollection ctls) {
        foreach (Control ctl in ctls) {
            var text = this.GetError(ctl);
            if (!string.IsNullOrEmpty(text)) {
                errors.Add(ctl, text);
                base.SetError(ctl, "");
            }
            copyErrors(ctl.Controls);
        }
    }

    private Dictionary<Control, string> errors = new Dictionary<Control, string>();

    // Plumbing to hook the form's Load event
    [Browsable(false)]
    public new ContainerControl ContainerControl {
        get { return base.ContainerControl; }
        set {
            if (base.ContainerControl == null) {
                var form = value.FindForm();
                if (form != null) form.Load += initialize;
            }
            base.ContainerControl = value;
        }
    }

    public override ISite Site {
        set {
            // Runs at design time, ensures designer initializes ContainerControl
            base.Site = value;
            if (value == null) return;
            IDesignerHost service = value.GetService(typeof(IDesignerHost)) as IDesignerHost;
            if (service == null) return;
            IComponent rootComponent = service.RootComponent;
            this.ContainerControl = rootComponent as ContainerControl;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)