Hello guys!

I need another help now. My last topic was about editing a image, which I already get working. Now I need a few things:

  1. Open a folder, add every image that is inside it to a listbox (filter all the other files).
  2. This one is a bit more tricky: I have two textboxes on my form. One of them is for the source folder. The other, destination. My goal is: when user types a path, it stores the typed value. Doing it char-by-char (eg, every char the user types gets stored) can give me some problem? Like too much memory in use, or something? Also, it would recognize if I'd hit backspace?
  3. Still on the textboxes thing, the second textbox is for the destination. If the user type a path, it would store, like above. But, if user simple types a folder name (and not the full path), the program would assume that it is on the same folder of source. It could be easily done by setting the two textboxes text to the same. But if user just clear everything, it would have to be manually set. How can I do it?
  4. And, at last, how can I only work on some selected items on a listbox? Like, I have 20 items inside a listbox, but only 12 are selected. So, when I do something that uses the listbox contents as source, it would only use the selected ones.

Thanks in advance, I'll be here trying to find out by myself, but waiting for responses! :D

[please correct me if I made any typo too, I want to improve my english :)]

You do not need to store every key the user types; the TextBox will do that for you. Just use the Text property when you need it.

If you use an OpenFileDialog the user can select the source file.

If you use a FolderBrowserDialog the user can select a destination folder.

If you still allow the user to type in a file name or folder path then you need to validate them using the System.IO.File.Exists and System.IO.Directory.Exists methods.

You could have a default folder or root folder which you add to any non-full paths the user gives you.

The ListBox has a SelectedItems property that contains only the selected items from the ListBox.

commented: Good explanation! +14

You do not need to store every key the user types; the TextBox will do that for you. Just use the Text property when you need it.

Oh, that's right! How could I forget this!!

If you use an OpenFileDialog the user can select the source file.

But if I want a mixed thing, kinda like I can select both a directory (will fetch all the files inside it) or files? OpenFileDialog doesn't do this, don't it?

If you use a FolderBrowserDialog the user can select a destination folder.

I thought about this one too.

If you still allow the user to type in a file name or folder path then you need to validate them using the System.IO.File.Exists and System.IO.Directory.Exists methods.

It would not look "unprofessional" if I don't allow the user to type? Many programs allow you to do that...

You could have a default folder or root folder which you add to any non-full paths the user gives you.

The ListBox has a SelectedItems property that contains only the selected items from the ListBox.

Thanks again, I'll look a bit more on MSDN and I'll try to implement it.

Hi, Im doing some code for you.
I have salved the 1st point (I am using "FolderBrowserDialog" object - which is only meant to choose a folder, so no files showed; thisis best for you since you want to get only spcific files.

Lets go to 2. point:
Why would you have then two textBoxes. Didnt you say in 1st point that you have a button to choose the directory? Why then having a directory path in the source textBox? The source path you will choose with that button.
We can do this way:
- two textBoxes
- two buttons on the end of those textBoxes
- on clicking on each button, FolderBrowserDialog will open and you will choose the destinations (for source and for destination path).


4. point is simple you will work only with "SelectedItems".

What do you think?

That's exactly what I have here =) Two textboxes with one button for each.

Also, ignore that picture box. I'm using it just for debugging/testing purposes.

LOL - your form look EXACTLY the same as mine :) , but I mean EXACTLY the same. Even buttons have three dots inside. Unbelivable.

So I can give you the code:

List<FileInfo> listOfFiles;
        public Form1()
        {
            InitializeComponent();
            listBox1.SelectionMode = SelectionMode.MultiSimple;
        }

        private void buttonSource_Click(object sender, EventArgs e)
        {
            listOfFiles = new List<FileInfo>();
            FolderBrowserDialog openFileDialog1 = new FolderBrowserDialog();
            openFileDialog1.SelectedPath = @"C:\";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string dirPath = openFileDialog1.SelectedPath;
                //set the path to the textBOx:
                textBox1.Text = dirPath;
                //filter for all images:
                string[] filters = { "*.jpg", "*.bmp", "*.png" };

                //get all the imeges to a list<T>:
                foreach (string filter in filters)
                {
                    FileInfo[] files = new DirectoryInfo(dirPath).GetFiles(filter);
                    foreach (FileInfo file in files)
                    {
                        listOfFiles.Add(file);
                    }
                }
            }
            //adding file names to listBox:
            foreach (FileInfo file in listOfFiles)
                listBox1.Items.Add(Path.GetFileNameWithoutExtension(file.FullName));
        }

        private void buttonDestination_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog openFileDialog1 = new FolderBrowserDialog();
            openFileDialog1.SelectedPath = @"C:\";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string dirPath = openFileDialog1.SelectedPath;
                //set the path to the textBOx:
                textBox2.Text = dirPath;               
            }
        }

        private void buttonProcess_Click(object sender, EventArgs e)
        {
            //I will only show how to work with selected files from listBox:
            //my example will only count selected files:
            int counter = 0;

            for (int i = 0; i < listBox1.SelectedItems.Count; i++)
            {
                string name = listBox1.SelectedItems[i].ToString();
                //find the file in generic list:
                foreach (FileInfo file in listOfFiles)
                {
                    if (name == Path.GetFileNameWithoutExtension(file.Name))
                    {
                        counter++;
                        //you use the destinaton path here to do something (or you will copy or what ever)!!
                    }
                }
            }

            MessageBox.Show("You have selected " + counter + " files");
            listBox1.SelectedIndex = -1;
        }

Remember, a class variable generic list<T> where T is FileInfo class, has all the files selected from the Source path.
And when you will do the ListBox selectedIndexChanged event, refer to this list.
FileInfo has all the data of each file (fullfile path, name, extension,..., actually all).

Remember, a class variable generic list<T> where T is FileInfo class, has all the files selected from the Source path.
And when you will do the ListBox selectedIndexChanged event, refer to this list.
FileInfo has all the data of each file (fullfile path, name, extension,..., actually all).

I didn't understant a letter of this :D I forgot to say that I'm kinda new to programming. Just now on college I'm having Pascal, but I learnt VB.Net by my own (just check VB.Net forums and you'll understand :)) but I read somewhere that C# is more powerful, so I switched.

And I recieved an error on the List<> part. what namespace it uses? I removed a lot of useless ones, but now I need them back xD

Hmm, then we have a problem here.
The code is meant to work in Win form.
About the generic list, it uses a "System.Collections.Generic" namespace (reference).

Put this code into a win form, and dont just copy/paste it, but you have to add event for controls, like (buttonSource_Click, ...), and then paste the code from here to your event.

commented: Helpfull! +14

Don't meed to build a new form, just have to re-reference the System.Collections.Generic (which I removed)(:

I'll do this later, now I'm not at my computer, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.