Foresp
Foresp

Reputation: 425

Creating a label array-Trying it but whats wrong?

this is the code:

        Label[] labelxx = new Label[5];
        this.Controls.Add(labelxx[0]);
        labelxx[0] = new System.Windows.Forms.Label();
        labelxx[0].Text = "stuff";
        labelxx[0].Location = new System.Drawing.Point(250, 250);
        labelxx[0].ForeColor = Color.White;
        labelxx[0].BackColor = Color.Yellow;
        labelxx[0].Size = new System.Drawing.Size(35, 35);

as you can see, I have set pretty much everything... text, location, size, forecolor-backcolor(to make it more visible in case I cant see it) However this not working... whats wrong ?

Upvotes: 2

Views: 544

Answers (2)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234384

You haven't really added the label to the form. When you create an array of labels, all the elements in it will be initialized to null. And then when you add one of those elements to the form, you're just adding null. You need to add the label to the form only after you created it.

    Label[] labelxx = new Label[5];
    labelxx[0] = new System.Windows.Forms.Label();
    labelxx[0].... // blah blah set everything
    this.Controls.Add(labelxx[0]);

Upvotes: 1

JAiro
JAiro

Reputation: 5999

try this

    Label[] labelxx = new Label[5];

    labelxx[0] = new System.Windows.Forms.Label();
    labelxx[0].Text = "stuff";
    labelxx[0].Location = new System.Drawing.Point(250, 250);
    labelxx[0].ForeColor = Color.White;
    labelxx[0].BackColor = Color.Yellow;
    labelxx[0].Size = new System.Drawing.Size(35, 35);

    this.Controls.Add(labelxx[0]);

Upvotes: 3

Related Questions