user1582483
user1582483

Reputation: 25

Newly created label will not show GUI

I am having some issues making my label show up in the gui... any thoughts?

private void addNewExcerciseButton_Click(object sender, EventArgs e)
        {

            int y = 305;
            int x= 61;

            string tempExcercise = excerciseTextBox.Text;
            excerciseTextBox.Clear();


           Label[] excerciseLabels = new Label[numExercises];



           for (int i = 0; i < numExercises; ++i)
           {
                excerciseLabels[i] = new Label();
                excerciseLabels[i].Text = ToString("{0}. {1}", i + 1, tempExcercise);;
                excerciseLabels[i].Location = new System.Drawing.Point(x, y);
                x += 10;
                y += 10;


                ++numExercises;
           }
}

thanks in advance.

numExercises is global.

Upvotes: 0

Views: 124

Answers (2)

KeithS
KeithS

Reputation: 71591

You have to add each new Label to the collection of Controls contained by a visible Control (such as your Form). You're creating and setting them up, but they aren't part of the GUI yet until they're in the control hierarchy.

Add the following line after setting the location of the label:

this.Controls.Add(exerciseLabels[i]);

Upvotes: 3

SLaks
SLaks

Reputation: 888047

You need to add the label to the GUI:

this.Controls.Add(excersizeLabels[i]);

As a side note, there is no point in using an array.

Upvotes: 2

Related Questions