Home > Back-end >  (design) 2 Elements blocking each other
(design) 2 Elements blocking each other

Time:11-24

simple question here:

I have a problem with one of my labels.

enter image description here

My problem is that the (!!!) label corresponding to the textbox "Datei Name" is being blocked by said textbox. Is there a setting that I have missed that puts said label behind/in front of the textbox?

How the (this part of the) program works: When something is incorrectly entered in one of the textboxes a label containing (!!!) and a Messagebox show up which tell the user where the error is located.

CodePudding user response:

As i mentioned in the comment to the question, i'd use enter image description here

When you move cursor over icon, you'll see error details:

enter image description here

If you've got LinqPad, you can use this script to check how it works:

void Main()
{
    MyForm mf = new MyForm();
    mf.Show();
}

// Define other methods and classes here
public class MyForm: Form
{
    private TextBox TxtDateiName = null;
    private TextBox TxtDateiNr = null;
    private Label Label1 = null;
    private Label Label2 = null;
    private Button BtnValidate = null;
    Dictionary<Control, ErrorProvider> ValidatedControls = null;
    
    public MyForm()
    {
        Initialize();
    }
    
    private void Initialize()
    {
        this.Size = new Size(250, 180);
        this.MinimizeBox = false;
        this.MaximizeBox = false;
        this.Text = "ErrorProvider Example";
        Label1 = new Label(){Text="Datei Name:", Location =new Point(10,12), AutoSize = true};
        Label2 = new Label(){Text="Datei Nr:", Location =new Point(10,42), AutoSize = true};
        TxtDateiName = new TextBox(){Name="TxtDateiName", Size= new Size(140,24), Location =new Point(78,10)};
        TxtDateiNr = new TextBox(){Name="TxtDateiNr", Size= new Size(140,24), Location =new Point(78,42)};
        BtnValidate = new Button(){Size = new Size(220, 48), Text = "Validate", Location = new Point(10, 78) };
        BtnValidate.Click  = BtnValidate_Click;
        this.Controls.Add(Label1);
        this.Controls.Add(Label2);
        this.Controls.Add(TxtDateiName);
        this.Controls.Add(TxtDateiNr);
        this.Controls.Add(BtnValidate);
        
        ValidatedControls = new Dictionary<Control, ErrorProvider>();
        ValidatedControls.Add(TxtDateiName, new ErrorProvider(this));
        ValidatedControls.Add(TxtDateiNr, new ErrorProvider(this));
        ValidatedControls[TxtDateiName].SetIconAlignment(TxtDateiName, ErrorIconAlignment.MiddleRight);
        ValidatedControls[TxtDateiName].SetIconPadding (TxtDateiName, 2);
        ValidatedControls[TxtDateiName].BlinkRate = 1000;
        ValidatedControls[TxtDateiName].BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
        ValidatedControls[TxtDateiNr].SetIconAlignment(TxtDateiName, ErrorIconAlignment.MiddleRight);
        ValidatedControls[TxtDateiNr].SetIconPadding (TxtDateiName, 2);
        ValidatedControls[TxtDateiNr].BlinkRate = 1000;
        ValidatedControls[TxtDateiNr].BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
        
    }

    private void BtnValidate_Click(object sender, EventArgs e)
    {
        bool ans = true;
        foreach(var k in ValidatedControls.Keys)
        {
            switch(k.Name)
            {
                case "TxtDateiName":
                    ans = IsValidName(((TextBox)k).Text);
                    ValidatedControls[k].SetError(k, ans ? "" : "Name is wrong!");
                    break;
            
                case "TxtDateiNr":
                    ans = IsValidNumber(((TextBox)k).Text);
                    ValidatedControls[k].SetError(k, ans ? "" : "Number is wrong!");
                    break;
            }
        }
        
        ans = ValidatedControls.All(kvp=>kvp.Value.GetError(kvp.Key)=="");
        MessageBox.Show(ans ? "Form is valid!" : "Form contains errors!", "Information");
        if(ans) this.Close();
    }
    
    private bool IsValidName(string dName)
    {
        return Regex.IsMatch(dName, @"^(\w{3,})$");
    }
    
    private bool IsValidNumber(string dNumber)
    {
        return Regex.IsMatch(dNumber, @"^(\d{3,5})$");
    }
    

}

Do not forget to add references (F4) to: System.Windows.Forms.dll and then add the following namespaces:

System.Drawing
System.Windows.Forms

Final note:

This is just an example. So, the code is not optimized.

  • Related