将 PictureBox 数组添加到控件中

adding PictureBox array into Controls

我尝试将 PictureBox 数组添加到控件中,但只添加了一个 pictureBox。

Label[] l = new Label[15];
PictureBox[] pic1 = new PictureBox[15];
int y_value = 47;

for (int i = 0; i < 6; ++i)
{

    l[i] = new Label();
    l[i].Text = "Test Text";
    l[i].Font = new System.Drawing.Font("Calibri", 8, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(128)));
    l[i].ForeColor = System.Drawing.Color.White;
    l[i].BackColor = System.Drawing.Color.FromArgb(1, 0, 64);
    l[i].Size = new System.Drawing.Size(145, 20);
    l[i].Location = new Point(30, y_value);
    l[i].Anchor = AnchorStyles.Left;
    l[i].Visible = true;
    //this.Controls.Add(l[i]);

    pic1[i] = new PictureBox();
    pic1[i].Image = Image.FromFile(STR_SETTING_PATH + "\" + STR_IDEA_NO_XXXXX + "_01_nv.png");
    pic1[i].Size = new System.Drawing.Size(400, 332);
    pic1[i].Location = new Point(2, y_value - 10);
    pic1[i].Anchor = AnchorStyles.Left;
    pic1[i].Visible = true;
    //this.Controls.Add(pic1[i]);

    y_value += 37;
}

this.Controls.AddRange(l);
this.Controls.AddRange(pic1);

当我这样做时,标签显示正确,但只出现一个 PictureBox。我尝试更改 X,Y 位置,但没有帮助。调试器显示已初始化的 PictureBox 数组列表。有什么地方我做错了吗,或者动态添加上面带有标签的 PictureBox 的更好方法是什么。

我测试了你的代码,事实上它们确实有重叠。 (每次迭代将背景色中的红色增加 y_value / 2)。

将 PictureBox 的高度设置为 (f.e.) 20,您会发现它们显示得很好:

Label[] l = new Label[15];
PictureBox[] pic1 = new PictureBox[15];
int y_value = 47;

for (int i = 0; i < 6; ++i)
{

    l[i] = new Label
    {
        Text = "Test Text",
        Font = new System.Drawing.Font("Calibri", 8, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte) (128))),
        ForeColor = System.Drawing.Color.White,
        BackColor = System.Drawing.Color.FromArgb(1, 0, 64),
        Size = new System.Drawing.Size(145, 20),
        Location = new Point(30, y_value),
        Anchor = AnchorStyles.Left,
        Visible = true
    };

    pic1[i] = new PictureBox
    {
        Size = new System.Drawing.Size(400, 20),
        Location = new Point(2, y_value - 10),
        Anchor = AnchorStyles.Left,
        Visible = true,
        BackColor = Color.FromArgb(y_value/2, 0, 0)
    };

    y_value += 37;
}

this.Controls.AddRange(l);
this.Controls.AddRange(pic1);