dotnet / dotnet/winforms

[Dark Mode] Some toolstrip colors don't change when switching color mode

Open
#12,027 8 comments 0 reactions 0 assignees View on GitHub
area-DarkMode help wanted
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### .NET version

9.0.100-rc.2.24430.10

### Did it work in .NET Framework?

No

### Did it work in any of the earlier releases of .NET Core or .NET 5+?

Dark mode is a new feature in net9

### Issue description

Some toolstrip colors don't change when the SystemColorMode is changed while the app is running, even after re-creating the form. (It works fine when setting the SystemColorMode at app start.)

Correct (when starting the app in dark mode):
![image](https://github.com/user-attachments/assets/972c4849-c700-4c00-842b-f6ac64f8b0f4)

Incorrect (when starting the app in light mode, switching to dark mode, and re-creating the form):
![image](https://github.com/user-attachments/assets/422e9029-502f-46bd-bb52-06b680e14300)

I believe the root cause of the issue is here, where there is a static cache of brushes with an index based on the `KnownColor` enum (e.g. `KnownColor.ControlText`). I imagine the solution involves purging these caches when the color mode is changed.

https://github.com/dotnet/winforms/blob/b6695d6cd48019890b785ea32684df47297bc2a5/src/System.Drawing.Common/src/System/Drawing/SystemBrushes.cs#L53

Also here has a similar problem, though I haven't evaluated if there are further impacts from these static caches.
https://github.com/dotnet/winforms/blob/b6695d6cd48019890b785ea32684df47297bc2a5/src/System.Drawing.Common/src/System/Drawing/SystemPens.cs#L54

### Steps to reproduce

1. Start the application with `Application.SetColorMode(SystemColorMode.Classic)`
2. Call `Application.SetColorMode(SystemColorMode.Dark)`
3. Close and re-open a form with a toolbar
4. Observe that ToolStripSeparator and dropdown arrows on ToolStripSplitButton have the wrong color

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.