Question

I was developing a WinForm application for a class and I ran into a bug that I can't seem to find the root of. When I run the application everything works except for an error label that was supposed to come up with incorrect user input. At first I thought I had written the event handler for it wrong, so I stopped hiding it at startup but the label is still missing. I'm not sure if I'm missing something in some back-end file or if I'm just missing something really obvious.

This is the function that creates the label.

private void InitializeErrorLabel()
{
    int width = 200, height = 13,
        anchorY = this.Label.Location.Y - this.Label.Size.Height - 3;

    // Initialize Component
    this.ErrorLabel.AutoSize = true;
    this.ErrorLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold,
        System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.ErrorLabel.ForeColor = System.Drawing.Color.Red;
    this.ErrorLabel.Location = new System.Drawing.Point((XSize - width) / 2, (anchorY - height));
    this.ErrorLabel.Anchor = System.Windows.Forms.AnchorStyles.Top;
    this.ErrorLabel.Name = "ErrorLabel";
    this.ErrorLabel.Size = new System.Drawing.Size(width, height);
    this.ErrorLabel.Text = "Invalid User ID. Please try again!";
    return;
}

and this is the function that initializes my controls:

private void InitializeComponent()
{
    this.UserInput = new System.Windows.Forms.TextBox();
    this.SwitchMajor = new System.Windows.Forms.RadioButton();
    this.SwitchToCS = new System.Windows.Forms.CheckBox();
    this.SwitchToCE = new System.Windows.Forms.CheckBox();
    this.KeepMajor = new System.Windows.Forms.RadioButton();
    this.AcceptValues = new System.Windows.Forms.Button();
    this.Label = new System.Windows.Forms.Label();
    this.ErrorLabel = new System.Windows.Forms.Label();
    this.SuspendLayout();

    // Initialize Components
    this.InitializeLabel();
    this.InitializeMainWindow();
    this.InitializeUserInput();
    this.InitializeSwitchMajorBtn();
    this.InitializeChangeToCSBtn();
    this.InitializeChangeToCEBtn();
    this.InitializeAcceptValuesBtn();
    this.InitializeErrorLabel();

    this.ResumeLayout();
    this.PerformLayout();
    return;
}

Again, not really sure what I'm doing wrong here. Any help would be appreciated.

Thanks,

Was it helpful?

Solution

In what control are you adding your errorlabel ?

A normal label initialization should look like

private System.Windows.Forms.Label ErrorLabel;

this.ErrorLabel = new System.Windows.Forms.Label();

this.groupBox2.Controls.Add(this.ErrorLabel);

this.ErrorLabel.AutoSize = true;
this.ErrorLabel.Location = new System.Drawing.Point(8, 59);
this.ErrorLabel.Name = "ErrorLabel";
this.ErrorLabel.Size = new System.Drawing.Size(55, 13);
this.ErrorLabel.TabIndex = 69;
this.ErrorLabel.Text = "Address 2";
this.ErrorLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;

Important

The label must be added to a control like my line three. Your control can be a form in your case. Its a groupbox in my case and the group box itself must be added to myform and myform must be visible .

 this.groupBox2.Controls.Add(this.ErrorLabel);

OTHER TIPS

I don't see a place where you're adding the label to the form's collection of controls:

this.Controls.Add(this.ErrorLabel);

If it's not a member of the Controls collection, you won't see it.

Related to that, I wouldn't be surprised if the other buttons, checkboxes, etc you're defining are not showing up on the form either, for the same reason.

Normally this would be taken care of by the Designer.cs file automatically.

This might be an obvious answer, but it totally went over my head. I verified that my code had the this.groupBox2.Controls.Add(this.ErrorLabel); like others have mentioned here. I couldn't figure out why I wasn't seeing my label. My label was an asterisk *, and in the designer window, I size it pretty small, but I was able to see the * in the designer window. Turns out I just had to increase it's area in the designer window since it was to small when I actually rendered the App.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top