如何自动创建图片框?

时间:2013-03-03 12:24:03

标签: c# winforms picturebox

我想自动创建PictureBox。如何在代码中更改此内容:

private void button1_Click(object sender, EventArgs e)
{
    PictureBox[] box = new PictureBox[textBox1.Text.Length];
    for(int j=0;j<textBox1.Text.Length;j++)
    box[0] = pictureBox1;
    box[1] = pictureBox2;
    box[2] = pictureBox3;
    for (int i = 0; i < textBox1.Text.Length; ++i) 
    box[i].Image = Image.FromFile(string.Format(@"c:\obrazki\{0}.jpg",textBox1.Text[i]));
}

1 个答案:

答案 0 :(得分:0)

您应该使用FlowLayoutPanel控件来保存PictureBox控件。然后代码看起来像这样:

void button1_Click(object sender, EventArgs e) {
  while (flowLayoutPanel1.Controls.Count > 0) {
    flowLayoutPanel1.Controls[0].Dispose();
  }

  for (int i = 0; i < textBox1.Text.Length; ++i) {
    PictureBox pb = new PictureBox();
    pb.Image = Image.FromFile(string.Format(@"c:\obrazki\{0}.jpg",textBox1.Text[i]));
    flowLayoutPanel1.Controls.Add(pb);
  }
}