【问题标题】:How to display a Labels 'Error on ErrorProvider1'如何显示标签“ErrorProvider1 上的错误”
【发布时间】:2014-10-08 15:35:07
【问题描述】:

目标

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

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

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

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

【问题讨论】:

    标签: vb.net winforms errorprovider


    【解决方案1】:

    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;
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 对,我明白这一点。但我试图让我的应用程序可以用法语/英语本地化。因此,我需要一个地方来存储两种语言的信息,具体取决于用户使用的文化信息......我认为Tag 属性可以保存这些信息,我对其进行了测试并且它有效......如果你想改变您的回答包括ErrorProvider.SetError(lblErr, lblErr.Tag) 错误所在的位置,我很乐意接受您的回答。
      • @Alex 感谢您提供有关如何存储字符串的信息。答案已更新。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多