Add label to Panel programmatically

后端 未结 2 865
误落风尘
误落风尘 2021-01-13 16:25

So I have a form, and I want to add some Panels with some controls(labels, and radiobuttons) when the form loads.
And I want to do it from the code, of course(it\'s for

2条回答
  •  甜味超标
    2021-01-13 16:58

    enter image description here

    private void button2_Click(object sender, EventArgs e)
        {
            int X = 153;
            int Y = 34;
            for (int i = 1; i < 4; i++)
            {
                Panel pnl = new Panel();
                pnl.SuspendLayout();
                pnl.Location = new Point(X, Y);
                pnl.Name = "pnl"+i;
                pnl.Size = new Size(200, 57);
                pnl.BorderStyle = BorderStyle.FixedSingle;
    
                Label lbl = new Label();
                lbl.Location = new Point(X - 100, Y - 17);
                lbl.Name = "lbl" + i;
                lbl.Size = new Size(75, 23);
                lbl.Text = "lable_" +i;
    
                pnl.Controls.Add(lbl);
                pnl.ResumeLayout(false);
    
                this.Controls.Add(pnl);
    
                Y = Y + 95;
            }
        }
    
    why not display label2 & label3?
    

提交回复
热议问题