Programmatically copy X number of panels within Form 以编程方式在表单内复制X个面板

huangapple go评论63阅读模式
英文:

Programmatically copy X number of panels within Form

问题

I searched other SO articles but couldn't find a solution that works in my case.

My objective is to take a copy of panel1, which I have already created and formatted within Form1 and duplicate it on the Form1 n number of times.

I would like to space them out a given vertical distance as well. Below is the code I am using, it compiles without error however when I open my form it is not showing anything other than the original panel I manually created.

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        int x = 100;
        this.Controls.Add(panel1);
        panel1.Location = new System.Drawing.Point(10, x);
        x += 200;
    }
}

EDIT: I have updated the code based on suggestions below but is still not working.

private void Form1_Load(object sender, EventArgs e)
{
    int y = 450;
    for (int i = 3; i < 6; i++)
    {
        Panel panel2 = new Panel();
        //Set Properties
        panel2.Controls.Add(this.button10);
        panel2.Controls.Add(this.dataGridView1);
        panel2.Location = new System.Drawing.Point(4, y);
        panel2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
        panel2.Name = "panel" + i;
        panel2.Size = new System.Drawing.Size(2754, 279);
        panel2.TabIndex = 0;
        y += 300;
        this.Controls.Add(panel2);
    }
}
英文:

I searched other SO articles but couldn't find a solution that works in my case.

My objective is to take a copy of panel1, which I have already created and formatted within Form1 and duplicate it on the Form1 n number of times.

I would like to space them out a given vertical distance as well. Below is the code I am using, it compiles without error however when I open my form it is not showing anything other than the original panel I manually created.

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        int x = 100;
        this.Controls.Add(panel1);
        panel1.Location = new System.Drawing.Point(10, x);
        x += 200;
    }
}

EDIT: I have updated the code based on suggestions below but is still not working.

    private void Form1_Load(object sender, EventArgs e)
    {
        int y = 450;
        for (int i = 3; i < 6; i++)
        {
            Panel panel2 = new Panel();
            //Set Properties
            panel2.Controls.Add(this.button10);
            panel2.Controls.Add(this.dataGridView1);
            panel2.Location = new System.Drawing.Point(4, y);
            panel2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            panel2.Name = "panel" + i;
            panel2.Size = new System.Drawing.Size(2754, 279);
            panel2.TabIndex = 0;
            y += 300;
            this.Controls.Add(panel2);
        }
    }

答案1

得分: 0

    int x = 100;
    for (int i = 0; i < 5; i++)
    {
        UserControl1 uc1 = new UserControl1();
        this.Controls.Add(uc1);
        uc1.Location = new System.Drawing.Point(10, x);
        x += 200;
    }

首先,你需要将int x = 100;这一行移出循环,否则它会将每个面板定位在相同的坐标上。
其次,你不能多次添加相同的组件,所以我建议创建一个UserControl,然后进行实例化。
最后,我想补充一下,变量x被用于Y坐标(我不知道这是否是有意的)。

英文:
    int x = 100;
    for (int i = 0; i < 5; i++)
    {
        UserControl1 uc1 = new UserControl1();
        this.Controls.Add(uc1);
        uc1.Location = new System.Drawing.Point(10, x);
        x += 200;
    }

Firstly, you have to move the line int x = 100; out of the loop, or it will position every panel at the same coordinate.
Secondly, you can't add the same component multiple times, so I recommend creating a UserControl which you can then instantiate.
As a last note, I wanted to add that the variable x is used for the Y-Coordinate(I don't know whether that's intentional).

答案2

得分: -1

这是因为您没有复制面板控件。您只是多次添加相同的对象,然后在同一对象上设置位置。

Panel panelToAdd = new Panel();
panelToAdd.Location = new System.Drawing.Point(10, x);
// 设置您想要的任何属性
this.Controls.Add(panelToAdd);
英文:

This is because you are not duplicating the panel control. You are just adding the same object multiple times and then setting the location on the same object.

Panel panelToAdd = new Panel();
panelToAdd.Location = new System.Drawing.Point(10, x);
//Set whatever properties you want
this.Controls.Add(panelToAdd);

huangapple
  • 本文由 发表于 2023年5月22日 22:11:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定