Add label to Panel programmatically

后端 未结 2 843
误落风尘
误落风尘 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:35

    Add the panel just created to the Form.Controls collection

    private void VizualizareTest_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 4; i++)
        {
            Panel pan = new Panel();
            pan.Name = "panel" + i;
            ls.Add(pan);
            Label l = new Label();
            l.Text = "l"+i;
            pan.Location = new Point(10, i * 100);
            pan.Size = new Size(200, 90);  // just an example
            pan.Controls.Add(l);
            this.Controls.Add(pan);
    
        }
    }
    
    0 讨论(0)
  • 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?
    
    0 讨论(0)
提交回复
热议问题