Dark mode discrepancy in file dialogs in Windows Forms (.NET)

176 views Asked by At

Writing an application in C#/.NET (Windows Forms/.NET Framework 4.7.2)

When I invoke a SaveFileDialog form within my application, it seems to honor the system's settings (a light dialog shows when dark mode is disabled globally, a dark dialog shows when dark mode is enabled system-wide).

Within the same application, the OpenFileDialog and FolderBrowserDialog are always shown in "light mode", regardless of the system settings.

Is there a way to force OpenFileDialog and FolderBrowserDialog to honor the system's theme?

EDIT 1: It seems that the SaveFileDialog control invokes the system's native Save Dialog, whereas the OpenFileDialog (and possibly the FolderBrowserDialog) controls in .NET are wrappers over the native system dialogs. In that regard, an acceptable "workaround" would be to invoke the native dialogs from within .NET (possibly using Windows API), assuming that the native dialogs would honor the system's dark mode settings by default.

EDIT 2: Regarding OpenFileDialog, and after coming across this SO question where one of the comments suggested that having ShowHelp enabled was causing an "older" dialog version to be displayed, I managed to get the "modern" dialog to show. Although I did not have ShowHelp enabled myself, I did have ShowReadOnly enabled. Disabling it did the trick. Not sure if this behavior is documented but it sure isn't obvious. As far as this question is concerned, while the OpenFileDialog issue is now resolved, the FolderBrowserDialog one remains.

1

There are 1 answers

0
NineBerry On BEST ANSWER

For the OpenFileDialog to support Dark Mode, make sure its AutoUpgradeEnabled property is set to True. Also make sure its ShowReadOnly and ShowHelp properties are set to false. The modern dialog variant doesn't support these ui elements.

The FolderBrowserDialog also has an AutoUpgradeEnabled that allows it to support Dark Mode, but only starting with .net Core. This is not supported in .Net Framework up to 4.8.

As long as you remain with .net Framekwork 4.x, replace the FolderBrowserDialog with some other component. There are a ton open source variants of the FolderBrowserDialog or variants of OpenFileDialog with support for only selecting folders out there.

You can also write your own variant of the OpenFileDialog that supports only picking folders based on the IFileDialog API. Set the FOS_PICKFOLDERS option. See for example Trying to open a file dialog using the new IFileDialog and IFileOpenDialog interfaces in C# with minimal code or Show detailed Folder Browser from a PropertyGrid for possible implementations.