Search code examples
c#vb.netforeachflowlayoutpanel

How to get all the data in flowlayout panel and copy to folder


i manage to do when i browse multiple image path in flowayoutpanel, and click on button save so, all the image path will save to a folder, my problem is i can browse the image path and show in flowlayoutpanel, but i dun know how to get the image path from it and copy to folder, any suggestion to do it?

*this is how i browse image path and show in 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);

    }
}

Solution

  • Obviously you have to store the filenames somewhere, either in a textbox like you did (although I would recommend tb.Text=file instead!), or you are using a separate List<string> (I would recommend the latter! Using a form for storing things is mostly no good idea).

    To get the files simply iterate either foreach(Control c in flowLayoutPanel1.Items) and then go down the SubControls to your textbox or use the separate list.

    The copy you can do with File.Copy(src, dest).