Visual Studio theming not applied correctly to custom Control property's UITypeEditor

gb 66 Reputation points
2024-05-29T19:27:50.2633333+00:00

Visual Studio: 2022 Community (17.10.0)

Visual Experience Color Theme: Dark

I'm working on a custom UITypeEditor (drop-down control) for a custom type that's a part of my custom control. When I trigger the UITypeEditor to drop down, I can see it in the default Windows control theme colors for an instant, then Visual Studio seems to apply my own theme colors (dark theme) causing only part of the control to be themed in line with Visual Studio. Specifically, it seems to miss the ForeColor property of my CheckBoxs.

User's image

This is the custom UITypeEditor:

public class ManualInputCollectionEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.DropDown;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if (value.GetType() != typeof(ManualInputCollection))
            return value;

        IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(
            typeof(IWindowsFormsEditorService));
        if (edSvc != null)
        {
            ManualInputCollectionControl collectionControl = new ManualInputCollectionControl(
                (ManualInputCollection)value);
            edSvc.DropDownControl(collectionControl);
            return collectionControl.Collection;
        }

        return value;
    }

    private class ManualInputCollectionControl : UserControl
    {

        public readonly ManualInputCollection Collection;

        public ManualInputCollectionControl(ManualInputCollection collection)
        {
            Collection = collection;                

            FlowLayoutPanel flp = new FlowLayoutPanel();
            flp.FlowDirection = FlowDirection.TopDown;
            flp.AutoSize = true;
            flp.AutoSizeMode = AutoSizeMode.GrowAndShrink;

                foreach (ManualInput item in Collection)
                {
                    CheckBox cb = new CheckBox
                    {
                        Name = Text = item.Name,
                        Tag = item,
                        Text = item.Name,
                        Checked = item.Enabled,
                        FlatStyle = FlatStyle.System
                    };
                    cb.CheckedChanged += Cb_CheckedChanged;
                    flp.Controls.Add(cb);
                }

            AutoSize = true;
            AutoSizeMode = AutoSizeMode.GrowAndShrink; 
            Controls.Add(flp);
        }          
        private void Cb_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = (CheckBox)sender;
            ((ManualInput)cb.Tag).Enabled = cb.Checked;
        }
    }
}

Troubleshooting steps: Previously, I didn't have the FlatStyle property assignment. A representative in the Developer Community recommended that I assign it to the System property (default would've been standard), although that did not seem to change the result. I've also tried changing the UITypeEditor to a Modal UITypeEditorStyle, which causes it to launch as a modal dialog in the default Forms theming which is expected but not exactly desired.Based on my code and the image above, am I doing anything wrong, or is this a Visual Studio issue?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,864 questions
{count} votes

1 answer

Sort by: Most helpful
  1. gb 66 Reputation points
    2024-05-29T19:33:45.6166667+00:00

    While I was writing the post, the A.I bot suggested that I try to override the BackColor of my custom control. That seems to have worked in that I can set it to a predictable color, although I'm still curious if there's any way to work with the Visual Studio theming engine.

    User's image

    0 comments No comments