` private void getbtn_Click(object sender, EventArgs e) // To generate Images
{
if (cmbDocType.SelectedIndex > 0)
{
if (con.State != ConnectionState.Open)
con.Open();
string directory = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory().ToString());
string FileNamePath = directory + "MembersDocuments\\" + GlobalValues.Member_ID + "\\" + cmbDocType.Text;
string[] list = Directory.GetFiles(FileNamePath);
if (list.Length > 0)
{
label1.Text = "";
PictureBox[] picturebox = new PictureBox[list.Length];
int y = 0;
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
if (x % 3 == 0)
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
picturebox[index].Location = new Point(x * 230 + 20, y);
picturebox[index].Size = new Size(200, 150);
x++;
picturebox[index].Size = new Size(200, 100);
picturebox[index].Image = Image.FromFile(list[index]);
picturebox[index].SizeMode = PictureBoxSizeMode.StretchImage;
picturebox[index].Click += new EventHandler(picturebox_Click);
cmbDocType_SelectedIndexChanged(picturebox[index], e);
this.Controls.Add(picturebox[index]);
}
}
else
{
label1.Text = "No Images to display";
label1.ForeColor = Color.Red;
}
con.Close();
}
else
{
MessageBox.Show("Please select the Document Type");
}
}
`谁能告诉我如何在新的调用中清除动态创建的pictureboxes中的先前图像(第一次调用的结果)。在进行新的调用时,以前的图像不应该是seen..in c#我有名为类型的组合框。比方说,如果我的组合框中有氨基酸,鸟。第一次呼叫时,将显示动物的图片,第二次选择组合框(即鸟类)时,将显示两种类型的图片。我需要一次显示单一类型的图片。在c#中,谢谢;
发布于 2017-04-15 23:23:30
正如TaW在评论中所建议的:
private List<PictureBox> PBs = new List<PictureBox>();
private void getbtn_Click(object sender, EventArgs e) // To generate Images
{
if (cmbDocType.SelectedIndex > 0)
{
foreach(PictureBox pb in PBs)
{
pb.Dispose();
}
PBs.Clear();
if (con.State != ConnectionState.Open)
con.Open();
string directory = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory().ToString());
string FileNamePath = directory + "MembersDocuments\\" + GlobalValues.Member_ID + "\\" + cmbDocType.Text;
string[] list = Directory.GetFiles(FileNamePath);
if (list.Length > 0)
{
label1.Text = "";
PictureBox PB;
int y = 0;
for (int index = 0; index < list.Length; index++)
{
PB = new PictureBox();
if (x % 3 == 0)
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
PB.Location = new Point(x * 230 + 20, y);
PB.Size = new Size(200, 150);
x++;
PB.Size = new Size(200, 100);
PB.Image = Image.FromFile(list[index]);
PB.SizeMode = PictureBoxSizeMode.StretchImage;
PB.Click += new EventHandler(picturebox_Click);
PBs.Add(PB);
this.Controls.Add(PB)
cmbDocType_SelectedIndexChanged(PB, e);
}
}
else
{
label1.Text = "No Images to display";
label1.ForeColor = Color.Red;
}
con.Close();
}
else
{
MessageBox.Show("Please select the Document Type");
}
}https://stackoverflow.com/questions/43425353
复制相似问题