我尝试将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的数组列表。有什么我做错了或者动态地在它上面添加带有Label的PictureBox的更好方法。
答案 0 :(得分:0)
我测试了你的代码,实际上它们确实重叠了。 (每次迭代时,背景颜色中的红色增加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);