清除(重置)动态生成的图片框

时间:2017-04-15 11:13:49

标签: c# winforms

` 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");
            }


        }
            `

任何人都可以告诉我如何在新通话中动态创建的图片框中清除之前的图像(第一次通话的结果)。在进行新的通话时,不应该看到以前的图像。在c#中 我有名为Type的组合框。 如果我在我的Combobox中有Aminals,Birds,那就说吧。 在第一次调用动物图片时,将显示第二次选择组合框,即鸟类,两种类型的图片都会显示出来。 我需要一次显示单一类型的图片。在c#中 感谢;

1 个答案:

答案 0 :(得分:0)

正如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");
    }
}