21

I know that if I set SelectedPath before I show the dialog I can get it to have a folder open by default when the dialog opens. However, the folder I want to use is very far down the list alphabetically. I have that same folder as one of my Libraries in Windows and it shows up at the of the listing, is there any way to have it default to the library version of the folder instead of the hard drive version of the folder?

Another potential solution would be if it did still use the drive version but it automatically scrolled the window down to where it was selected. Is there any way to do either of these solutions?


How it currently shows up

enter image description here

How I would like it to show up

enter image description here

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • If this problem is about auto-scroll, you might either type tab-tab-rightarrow, or program this, see http://stackoverflow.com/a/29691834/1845672 – Roland Feb 15 '16 at 11:55

4 Answers4

17

Set your root folder and selected path as such and it will auto-scroll there for you on the dialog opening:

FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.RootFolder = Environment.SpecialFolder.MyComputer;
dlg.SelectedPath = @"E:\Vetcentric";
dlg.ShowDialog();

enter image description here

The problem you run into is that if you watch the property assignments after selecting a folder located in the libraries hierarchy, it will still assign it to the genereic path that you would get via going through my computer.

KreepN
  • 8,528
  • 1
  • 40
  • 58
  • 1
    Interesting, using the designer to set the `RootFolder` and the `SelectedPath` it selected the folder and expanded out the tree, but it did not auto-scroll to the window. Moving those two lines of code out of the designer and in to my constructor made it do my desired behavior. Thanks! – Scott Chamberlain Dec 06 '11 at 21:45
  • 1
    Also another note to anyone else who reads this, If you select a path, press OK, then open the dialog again it will have the folder selected but it will not auto-scroll again. – Scott Chamberlain Dec 06 '11 at 21:49
  • @Scott Chamberlain: just as you said, this solution works only for the first time you open the Dialog. Does any one have a better solution ? – itsho Aug 23 '12 at 22:55
  • @itsho My guess is that the selected path changes once you make your first folder selection (obviously). You could probably just default it back to the same SelectedPath on some event tied to the control (such as a button or input field that causes the dialog to display in the first place) so that the behavior is the same each time. – KreepN Aug 24 '12 at 15:13
6

Use a Reset() call. This will make it auto-scroll.

        string prevpath = folderBrowserDialog1.SelectedPath;
        folderBrowserDialog1.Reset();
        folderBrowserDialog1.SelectedPath = bc.myWorkingDir;
        folderBrowserDialog1.ShowNewFolderButton = true;

        DialogResult dr = folderBrowserDialog1.ShowDialog();
        if (dr == DialogResult.OK || dr == DialogResult.Yes)
        {
            bc.myWorkingDir = folderBrowserDialog1.SelectedPath;
        }
        folderBrowserDialog1.SelectedPath = prevpath;
eyeching
  • 76
  • 1
  • 3
0

Just set the path Libraries\VetCentric... before you open should do it I think.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

The easiest way would probably be to put shortcuts to the folders you want into your starting folder. Then, just double click on the shortcut and it will take you to your folder.

Otherwise you will need to use the Shell Library API See: Using Libraries in your Program

Trisped
  • 5,705
  • 2
  • 45
  • 58