941,498 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1043
  • C# RSS
Jul 29th, 2010
0

FolderBrowserDialog: Expanding the selected directory ...

Expand Post »
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;  <---- 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.
            }
            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.
Last edited by Zinderin; Jul 29th, 2010 at 6:52 pm.
Similar Threads
Reputation Points: 22
Solved Threads: 8
Junior Poster in Training
Zinderin is offline Offline
76 posts
since Jun 2010
Jul 30th, 2010
0
Re: FolderBrowserDialog: Expanding the selected directory ...
Quote ...
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:
C# Syntax (Toggle Plain Text)
  1. f.SelectedPath = dirTextBox;
  2. SendKeys.Send("{TAB}{TAB}{RIGHT}");

HTH
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,023 posts
since Aug 2008
Jul 30th, 2010
0
Re: FolderBrowserDialog: Expanding the selected directory ...
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

C# Syntax (Toggle Plain Text)
  1. string Folder = string.Empty;
  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. FolderBrowserDialog fbd = new FolderBrowserDialog();
  5. if (Folder != string.Empty)
  6. {
  7. DirectoryInfo sub = new DirectoryInfo(Folder);
  8. //check for subdirectory
  9. if (sub.GetDirectories().Length > 0)
  10. sub = sub.GetDirectories()[0]; //if found, move to first
  11. fbd.SelectedPath = sub.FullName; //set dialog path to first child directory
  12.  
  13. }
  14.  
  15. if (fbd.ShowDialog() == DialogResult.OK)
  16. Folder = fbd.SelectedPath; //only update stored path if user clicks OK to avoid overwriting with SubDirectory
  17. }
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Jul 30th, 2010
0
Re: FolderBrowserDialog: Expanding the selected directory ...
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.
Reputation Points: 22
Solved Threads: 8
Junior Poster in Training
Zinderin is offline Offline
76 posts
since Jun 2010
Jul 30th, 2010
0
Re: FolderBrowserDialog: Expanding the selected directory ...
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.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Jul 31st, 2010
0
Re: FolderBrowserDialog: Expanding the selected directory ...
If you want such a control, use FolderView
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ghimangi is offline Offline
20 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: .NET Setting HelpProvider PopUp Location
Next Thread in C# Forum Timeline: C# and SQL connections





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC