2017-04-15 23 views
1
` 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# 我有名为类型的组合框。 可以说,如果我有Aminals,Birds在我的Combobox中。 将显示动物的第一次呼叫图片,并在第二次选择组合框时,即鸟类,这两种类型的图片都会显示。 我需要一次显示单一类型的图片。在c# 谢谢;要清除(重置)动态生成的图片框

+0

使用'DataGridView'与'DataGridViewImageColumn'而不是动态创建PictureBoxes。在哪里可以设置新图像,只需一行'yourDataGridView.DataSource = collectionOfImages' – Fabio

+0

将这些动态存储在列表中动态创建Picturenox!然后你清除图像(不要忘记也可以处理它们!!!)或删除PB自己.. – TaW

回答

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

非常感谢你的解决方案Idle_Mind,TaW和Fabio。现在它工作正常。 – Ren