TreeViewFolderBrowser icon indicating copy to clipboard operation
TreeViewFolderBrowser copied to clipboard

Preselect a directory and populate it on startup

Open AFract opened this issue 1 year ago • 4 comments

Hello Chris, I am using your control since a very long time in an old project, I have just tried to update it from the original CodeProject version to this Github enhanced edition (my final goal is to migrate it to .Net 7.0, it works great so far except for a few details). So thanks a lot for your great work !

Here's my problem, In the old version, it was easy to "preselect" a folder on startup on show its content directly. I want to have "Desktop" as root, and jump directly to a specific folder and expand it. I did this :

            var datasource = new TreeViewFolderBrowserDataProviderShell32();
            datasource.EnableContextMenu = true;
            datasource.ShowAllShellObjects = true;

            ctlFolderSelect.DriveTypes = DriveTypes.All;
            ctlFolderSelect.DataSource = datasource;
            ctlFolderSelect.RootFolder = Environment.SpecialFolder.Desktop;

            ctlFolderSelect.Populate(@"C:\"); // For example

With the new version, I get an ArgumentOutOfRangeException, by doing something close (I have slightly adapted my old code to attempt to match updated version) :

            var datasource = new TreeStrategyShell32Provider();
            datasource.EnableContextMenu = true;
            datasource.ShowAllShellObjects = true;
            
            ctlFolderSelect.CheckBoxBehaviorMode = CheckBoxBehaviorMode.None;
            datasource.DriveTypes = DriveTypes.All;
            ctlFolderSelect.DataSource = datasource;

            ctlFolderSelect.Populate(@"C:\"); // Throws ArgumentOutOfRangeException

When I open manually the folders in the control, everything works great.

Could you please tell me how to preselect a folder and expand it by code ? I didn't see any example of this in the "Demo" project and I found nothing by my own.

Thanks a lot

AFract avatar Aug 11 '23 18:08 AFract

Hi there

Nice to know that my code is still running after all those years. I myself left this project behind me many many years a go.

Interesting idea to port this code to .net 7.0, do you think this is really possible?

ChrisRichner avatar Aug 12 '23 10:08 ChrisRichner

Hello,

The only obvious broken thing is the "ContextMenu", which have been replaced by "ContextMenuStrip", and so one for related items. Apart classes names, a few things have changed in its API ("Open" event disappeared, replaced by "Opening" I think, etc).

Apart this, with Shell provider (I didn't tried others !), all main features seem to work properly, at least at first look.

My only problem (not related to .Net 7) is my question on how to jump to a specific folder on startup.

Do you know why it's broken, while it was previously working ? I thought it could be something to do on the "Datasource" instead of the treeview itself.

Regards

AFract avatar Aug 12 '23 12:08 AFract

Wow, that's good News!

Do you mind sharing your .net 7.0 code? I would love to have a Pull Request to update this old code base to .net 7.0 and have a look at the problem you're facing right now.

Deal? 😉

ChrisRichner avatar Aug 13 '23 06:08 ChrisRichner

Honestly, I won't have time to do a PR with fully working and tested .Net 7.0, I have 15 days to migrate 7 applications and your control is used in only one place of one of them. So I have just did a quick attempt with some custom adjustments, so it's not meaningful to share it as is.

However, I'd be glad to share with you the full process !

Microsoft have supplied some great tools to migrate any existing application to .Net 7.0, including new SDK .csproj format, removal of packages.config, etc. The app will do 95% of the work for you as long as your code is compliant, and most of your project seems fine in regard to this.

To install them, just open an Administrator cmd window and run :

dotnet tool install upgrade-assistant -g
dotnet tool install try-convert -g

Then, run the following command :

upgrade-assistant upgrade "C:\path\to\the.sln"

Select the target project (you will probably have to run the app several times to apply on each project), choose "in place" upgrade, and choose on next step .Net 7.0.

It will run fine for all your projects, update the csproj (again, you'll probably have to run it several time. Also, close Visual Studio before to run any of them).

Open the sln in Visual Studio, it will raise some errors :

  • You have [System.Drawing.ToolboxBitmap(typeof (System.Data.SqlClient.SqlDataAdapter))] in your Provider assembly. The attribute itself still exists but System.Data.SqlClient.SqlDataAdapter not (very old class) so it's not supported anymore. I guess you could just put any other icon instead of this one.

  • System.Security.Permissions.PermissionSet attribute is deprecated (I have just commented them)

  • Replace in TreeViewFolderBrowser.cs

            this.ContextMenu = new System.Windows.Forms.ContextMenu();
            this.ContextMenu.Popup += new System.EventHandler(OnContextMenu_Popup);

by (I believe)

            this.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
            this.ContextMenuStrip.Opening += OnContextMenu_Popup;

And ContextMenu.MenuItems.Clear(); by ContextMenuStrip.Items.Clear(); in another method of this class.

As you will notice, the upgrade tool have generated useful comments like "// TODO ContextMenu is no longer supported. Use ContextMenuStrip instead. For more details see https://docs.microsoft.com/en-us/dotnet/core/compatibility/winforms#removed-controls"

In my case the context menu seems to open and trigger its events, by it appears to be empty, I didn't investigate why (because I don't need it in my usage).

  • Your AboutBox window in demo project also have some ContextMenu, I guess same cure applies here than in treeview control itself.

  • I have specified in project settings to ignore hundreds of "CA1416" warnings. They are intended to warn you about what won't work outside of Windows. I didn't say porting to .Net 7 doesn't mean to be compatible with Mac and Linux :)

And here you go ! I believe all of this can be reproduced in 1 hour. This is all I have done. Of course, more tests are required to ensure everything works.

Adding a unit test project would also be a good practice if you wish to improve this great project :)

And of course an example of a working "folder preselection" as I desperately need it :D

Again, thanks a lot !

AFract avatar Aug 13 '23 13:08 AFract