开发者

Creating a label array-Trying it but whats wrong?

开发者 https://www.devze.com 2023-03-13 16:23 出处:网络
this is the code: Label[] labelxx = new Label[5]; this.Controls开发者_开发百科.Add(labelxx[0]); labelxx[0] = new System.Windows.Forms.Label();

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 ?


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


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]);
0

精彩评论

暂无评论...
验证码 换一张
取 消