Reputation: 37
I want to create a label and display it to the user, but I can not.
I have tried to copy the code for any label in InitializeComponent() ...
(I added a label to Form1 using the toolbox.)
partial class Form1
{
private System.Windows.Forms.Label label1;
private void InitializeComponent()
{
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
}
}
... and then to apply it to my label.
(I removed the label I added earlier.)
public partial class Form1 : Form
{
private Label label;
public Form1()
{
InitializeComponent();
label = new Label();
label.AutoSize = true;
label.Location = new System.Drawing.Point(0, 0);
label.Name = "label";
label.Size = new System.Drawing.Size(0, 0);
label.TabIndex = 0;
label.Text = "Test";
//label.Enabled = true;
label.Visible = true;
//label.Select();
//label.Show();
}
}
But it does not work. How to do ?
Upvotes: 1
Views: 1538
Reputation: 31
You need to add te label to add it to the forms' list of controls.
So, in your Form1()
function, add the follwoing after the label is created:
this.Controls.Add(label);
Upvotes: 3
Reputation: 3900
You forgot vital part, i.e. add label to the form's ControlCollection
:
this.Controls.Add(label);
Upvotes: 2