1

Windows Forms application using VS2022 CE. I have a 'browse' button for selecting the folder of interest, as shown in the following snippet:

        private void btn_Browse_Click(object sender, EventArgs e)
    {
        folderBrowserDialog1.Description = "Choose the folder containing crossword print files";
        folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyDocuments;

        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            tb_Folder.Text = folderBrowserDialog1.SelectedPath;
        }
    }

This all worked OK until I decided to add code to set the root directory for the search. After the 'RootFolder' property was set, the 'Documents' folder shows up in the dialog but it can't be expanded. What am I doing wrong here?

Before (without the RootFolder property line):

enter image description here

After (with the RootFolder property line): enter image description here

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
user3765883
  • 233
  • 1
  • 9
  • To be clear, is the issue just that the expand/collapse icon is missing, or that you can't even double-click on the Documents item to expand the folder? – Mike Hofer Sep 20 '22 at 18:17
  • For this one, set the `SelectedPath` property as well to one of the `RootFolder` subfolders. [Remarks](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.folderbrowserdialog.rootfolder?view=windowsdesktop-6.0#remarks). Specifically _as long as SelectedPath is an absolute path that is a subfolder of RootFolder..._. Try another root like `Environment.SpecialFolder.UserProfile`. No need here to set the `SelectedPath` property. – dr.null Sep 20 '22 at 19:13
  • Both: the expand/collapse icon is missing AND I can't double-click to expand the folder – user3765883 Sep 20 '22 at 22:51
  • dr.null: Using Environment.SpecialFolder.UserProfile works, isn't much better than the default behavior. What I was trying to do was set the start of the Browse operation to a sub-folder several layers down under 'My Documents'. I can set the SelectedPath as you suggested, but seems a bit counter-intuitive. – user3765883 Sep 20 '22 at 22:54
  • Yes, this is how it works. Create your own or use a 3rd party one. Some examples around https://www.codeproject.com/Articles/301943/TreeView-Explorer-using-VB-NET-2008, https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/creating-an-explorer-style-interface-with-the-listview-and-treeview?view=netframeworkdesktop-4.8&redirectedfrom=MSDN, https://www.codeproject.com/Articles/14570/A-Windows-Explorer-in-a-user-control. and much more. – dr.null Sep 21 '22 at 03:52

0 Answers0