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,
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);