So I have this program that involves the user using FolderBrowserDialog.

And it works fine ... but the rub is, when the user comes back around and uses the dialog again, the directory is highlighted, but not expanded, and he's asking that it be expanded.

LOL! For the life of me, I can't figure out how to expand the selected directory when the dialog is redisplayed.

private void dirSelect_Click(object sender, EventArgs e)
            FolderBrowserDialog f = new FolderBrowserDialog();
            if (dirTextBox.Text != string.Empty)
                f.SelectedPath = dirTextBox.Text;  [B]<---- Right here is where I not
                                                         only want to select the dir
                                                         he selected prviously, but
                                                         expand it for him as well so
                                                         he sees the sub-folders without
                                                         having to click on it.[/B]
            DialogResult result = f.ShowDialog();
            if (result == DialogResult.OK)
                //Add the selected directory to the textbox.
                dirTextBox.Text = f.SelectedPath + @"\";

I did try to programatically execute the "on click" event, but that just got me into all kinds of trouble.

As always, thanks guys.

Edited by Zinderin: n/a

6 Years
Discussion Span
Last Post by ghimangi

but that just got me into all kinds of trouble.

SendKeys isn't very reliable way to "control" the application, but you could try this:

f.SelectedPath = dirTextBox;



I'm always dubious about using SendKeys...as Teme says, its not the most reliable method of control.
Mine isnt a perfect solution either, but its a viable workaround (in my opinion).
Since you can only expand the directory if it has SubDirectories, the code below checks for their existance. If there are any SubDirectories then it sets the first as the SelectedPath. This will display the directory expanded as required, however, it will also mean that the highlighted folder is not the root folder previously selected. The only reason i can see for your user to require the folder to be expanded when the dialog displays is that they intend to select a subdirectory and are too lazy to click the arrow ;)

string Folder = string.Empty;
    private void button1_Click(object sender, EventArgs e)
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (Folder != string.Empty)
            DirectoryInfo sub = new DirectoryInfo(Folder);
            //check for subdirectory
            if (sub.GetDirectories().Length > 0)
                sub = sub.GetDirectories()[0]; //if found, move to first
            fbd.SelectedPath = sub.FullName; //set dialog path to first child directory


        if (fbd.ShowDialog() == DialogResult.OK)
            Folder = fbd.SelectedPath; //only update stored path if user clicks OK to avoid overwriting with SubDirectory

Hmmm, I'll give Ryshad's code a shot.

I posed this question more as a learning "thing" for me. And I'm glad to see I wasn't just missing something.

I was just trying to respond to his comment that his users are being too lazy to actually check if there is a more appropriate sub-directory, and just are just dumping everything into the parent.

I'm not even being paid for this ... its was just a favor for a friend, which turned out to require C# (that's why I'm learning it finally) ... and then all the other "also's" that always accompany any application that was just supposed to be simple.

But I didn't go into it blind ... after 30 years in this business, I knew it was coming ... once they have it and like it, there's always a bazillion "oh, and also..."

So yeah, this is more a learning thing for me ... thanks guys as always, you rock.

Marking this as solved.


Long term (when your getting paid) you could always roll your own FolderBrowser. Its just a form with a treeview after all :) If you make it yourself you have complete creative control.

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.