3

目标

每当我遇到错误时,我想显示我放入标签的“ErrorProvider1 上的错误”属性中的文本。请参阅以下标签的属性。

ErrorProvider1 上的错误

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

 If TextBox1.Text.Trim.Contains("'") Then
    ErrorProvider1.SetError(lblErr, ErrorProvider1.GetError(lblErr))
Else
    ErrorProvider1.SetError(lblErr, "")
End If

如何从 lblErr 检索“ErrorProvider1 上的错误”文本以将其显示在 ErrorProvider1SetError值中?

4

2 回答 2

8

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

  • ShowError(Control ctl, bool enable) 当enable参数为真时显示您在设计时输入的文本。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;
        }
    }
}
于 2014-10-08T17:05:19.910 回答
2

您的问题是当没有任何问题时您正在替换错误消息。正如您在下面的评论中所述,您将本地化错误消息存储在标签中Tag,因此您可以执行以下操作:

If TextBox1.Text.Trim.Contains("'") Then
    ErrorProvider1.SetError(lblErr, lblErr.Tag)
Else
    ErrorProvider1.SetError(lblErr, "")
End If

你是正确的ErrorProvider1.GetError(Control)用来获得价值。只是在检索它之前,您很可能将其替换为空字符串。

于 2014-10-08T15:46:56.340 回答