1

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 ?

1
  • 3
    You haven't added the label to a form or other parent that would allow it to be shown. Commented May 28, 2017 at 15:35

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

2

You forgot vital part, i.e. add label to the form's ControlCollection:

this.Controls.Add(label);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.