2017-10-19 54 views
0

当我在flowayoutpanel中浏览多个图像路径并点击按钮保存时,所有图像路径将保存到文件夹,我的问题是我可以浏览图像路径并显示在flowlayoutpanel中,但我知道如何从它获取图像路径并复制到文件夹,任何建议做到这一点?如何获取flowlayout面板中的所有数据并将其复制到文件夹

*我这是怎么浏览图片的路径,并显示在FlowLayoutPanel的

private void button1_Click_2(object sender, EventArgs e) 
     { 
      DialogResult dr = this.openFileDialog1.ShowDialog(); 
      if (dr == System.Windows.Forms.DialogResult.OK) 
      { 
       // Read the files 
       foreach (String file in openFileDialog1.FileNames) 
       { 
        // Create a PictureBox. 
        try 
        { 
         Panel panel = new Panel(); 
         PictureBox pb = new PictureBox(); 
         TextBox tb = new TextBox(); 
         Image loadedImage = Image.FromFile(file); 
         pb.Height = 200; 
         pb.Width = 200; 
         pb.Image = loadedImage; 
         pb.SizeMode = PictureBoxSizeMode.StretchImage; 
         tb.Text = openFileDialog1.FileName; 
         //panel.Controls.Add(pb); 
         panel.Controls.Add(tb); 
         panel.Height = 200; 
         panel.Width = 200; 
         flowLayoutPanel1.Controls.Add(panel); 

        } 
        catch (SecurityException ex) 
        { 
         // The user lacks appropriate permissions to read files, discover paths, etc. 
         MessageBox.Show("Security error. Please contact your administrator for details.\n\n" + 
          "Error message: " + ex.Message + "\n\n" + 
          "Details (send to Support):\n\n" + ex.StackTrace 
         ); 
        } 
        catch (Exception ex) 
        { 
         // Could not load the image - probably related to Windows file system permissions. 
         MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\')) 
          + ". You may not have permission to read the file, or " + 
          "it may be corrupt.\n\nReported error: " + ex.Message); 
        } 


       } 
      } 

private void button2_Click(object sender, EventArgs e) 
{ 

    foreach(TextBox tb in TextBoxes) 
    { 


     File.Copy(tb.Text, dest); 

    } 
} 

回答

0

很明显,你必须保存的文件名的地方,无论是在一个文本框,像你这样(虽然我会建议代替tb.Text=file!),或者您正在使用单独的List<string>(我会推荐后者!使用表单来存储事物大多不是好主意)。

要获取文件只需迭代foreach(Control c in flowLayoutPanel1.Items)然后沿着SubControls到您的文本框或使用单独的列表。

您可以使用File.Copy(src, dest)的副本。

+0

目前,您已将flowPanel作为子面板,并且每个面板都有一个文本框和一个picturebox(注释掉)作为它们的子节点。因此,现在的文本框应该是每个面板的第一个子控件 – casiosmu

+0

谢谢,那么如何在我的第二个按钮中的文本框flowlayoutpanel中定义文本? – tang

+0

请认为:你的'c.Text'会给你一个空的结果,当然它是面板的文本...但是你想从面板 – casiosmu

相关问题