如何自动创建图片框

本文关键字:创建 何自动 | 更新日期: 2023-09-27 18:02:11

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

如何自动创建图片框

你可能应该使用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);
  }
}