0

我想自动创建一个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]));
}
4

1 回答 1

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);
  }
}
于 2013-03-03T13:09:38.370 回答