首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >清除(重置)动态生成的图片框

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

Stack Overflow用户
提问于 2017-04-15 19:13:49
回答 1查看 95关注 0票数 1
代码语言:javascript
复制
` 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#中,谢谢;

EN

回答 1

Stack Overflow用户

发布于 2017-04-15 23:23:30

正如TaW在评论中所建议的:

代码语言:javascript
复制
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");
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43425353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档