Click here to Skip to main content
15,908,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on windows application. I am using 2 comboboxes, where first comboBox1 lists all the folders in the physical directory and comboBox2 lists all the files from the folders listed in comboBox1, when particular folder name is selected from comboBox1.

I am trying to figure out the way to list all the files from the folder in comboBox2 for every available folder and check count of files within the folder and when files in the folder reached end of file, selected index should move to next folder in the comboBox1.

As folder don't have same number of files in each folder I am trying something like recursion.

Any suggestions and help is appreciated?

ComboBox1: Lists all the folders from physical location.(Works fine)
C#
private void Form_Load(object sender, EventArgs e)
{
    DirectoryInfo di = new DirectoryInfo("@\\server\CAMR");
    paths = new String[di.GetDirectories().Count()];
    int i = 0;
    foreach (DirectoryInfo fi in di.GetDirectories())
    {
       comboBox1.Items.Add(fi.Name);
    }

    foreach (DirectoryInfo fi in di.GetDirectories())
    {
       paths[i] = fi.FullName;
       i++;
       comboBox2.Sorted = true;
    }
    comboBox1.Text = "";
}

ComboBox2: As of now I am doing it manually for each selected index from comboBox1
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

    if (comboBox1.SelectedIndex == 1)
    {
        DirectoryInfo dinf = new DirectoryInfo(@"\\server\CAMR\Mxxxxxxxxxxx");
        paths = new String[dinf.GetFiles().Count()];
        int i = 0;
        comboBox2.Items.Clear();
        pictureBox2.Image = Image.FromFile("@\\server\CAMR\Mxxxxxxxxxxx\SCN0001.tif");
        foreach (FileInfo finf in dinf.GetFiles())
        {
            comboBox2.Items.Add(finf.Name);
        }

        foreach (FileInfo finf in dinf.GetFiles())
        {
            paths[i] = finf.FullName;
            i++;
        }
        comboBox2.Text = "SCN0001.tif";
        txtovrbn.Text = comboBox1.Text;
        txtovrts.Text = DateTime.Now.ToString();
    }
}


I am trying to find recursion here, so it will take care of all the folder listed in comboBox1.
Posted
Updated 10-Nov-15 11:45am
v3
Comments
Matt T Heffron 9-Nov-15 20:32pm    
It sounds like you are using comboBox2 just to show all of the files from comboBox1's folder as you "process" them somehow.
("when files in the folder reached end of file, selected index should move" is what led me to this assumption.)

Can you clarify exactly what you're tying to accomplish? Perhaps show some of the relevant code you already have?
Use the "Improve question" above to do this.
Member 12076824 10-Nov-15 22:18pm    
Thanks for response, I am trying to list all the files for selected folder from the comboBox1 into ComboBox2. I am doing that manually for each selected index.
I am trying to have some kind of loop for that.

Patrice T 9-Nov-15 20:36pm    
Please no repost
BillWoodruff 9-Nov-15 20:37pm    
I think you are using the wrong Controls here, and suggest you a TreeView Control and a ListView Control, rather than two ComboBoxes. CodeProject has good articles on using a TreeView and ListView to emulate Windows Explorer. Just search.
Member 12076824 10-Nov-15 22:19pm    
Thanks Bill I will look into that.

1 solution

Use event SelectedValueChanged or SelectedIndexChanged on second combobox, check that currentIndex == combobox.Items.Count -1 then combobox1.SelectedIndex = combobox1.SelectedIndex +1


C#
protected void combobox2_SelectedIndexChanged(object sender, EventArgs e) {
    if (combobox2.SelectedIndex = combobox2.Items.count -1) {
            // full code for clarity, there are several shortcuts for this
            combobox1.SelectedIndex = combobox1.SelectedIndex +1

// this index change should reload cb2
    }
}
 
Share this answer
 
v2
Comments
Member 12076824 19-Nov-15 17:57pm    
Thanks Sinisa, this was helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900