How to display shortcuts error on ErrorProvider1 '

purpose

I want to display the text that I have entered in the "Error on ErrorProvider1" Label attribute when I receive an error. Following are the following tag attributes.

Error on ErrorProvider1

I am trying to display text in a red rectangle in my ErrorProvider1 function SetError(control, value)

.

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

      

How can I get the text "Error on ErrorProvider1" from lblErr to display it in the ErrorProvider1 value SetError

?

+2


source to share


2 answers


Your problem is that you are replacing the error message when nothing works. As noted in your comment below, you keep the localized error message in the label Tag

, so you can do the following:

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

      



You used correctly ErrorProvider1.GetError(Control)

to get the value. It's just that you will likely replace it with an empty string before you retrieve it.

+2


source


The ErrorProvider component is very inconvenient to use effectively. However this is a fix, I will give an example in C # that extends the component with some new features:

  • ShowError (Control ctl, bool enable) displays the text you entered at design time when the enable argument is true. An easy to use version of SetError ().
  • HasErrors returns true if all active warning icons are displayed. Conveniently in your OK button. Click event handler.
  • FocusError () sets focus to the first control that has a warning icon, if any. It returns false if there are no warnings left.
  • SetError () is a replacement for ErrorProvider.SetError (). You only need it if you add any controls after the Load Event is fired, or if you need to change the alert text.


Add a new class to your project and paste the code shown below. Compilation. Remove it from the top of the toolbar onto the form. Design-time behavior is identical. Modestly tested.

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

      

+5


source







All Articles