Question

Got a bit of a run with PropertyGrid control and now stumbled upon a curious problem. I've got an Uint32 property in a class which is a bit mask. So I decided to create a custom dropdown UserControl with 32 buttons to make Uint32 editable. Here is the class (without button click handlers):

class MaskEditorControl : UserControl, IIntegerMaskControl
{
        public MaskEditorControl()
        {
            InitializeComponent();
        }

        public UInt32 ModifyMask(IServiceProvider provider, UInt32 mask)
        {
            IWindowsFormsEditorService editorService = null;
            if (provider != null)
                editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;

            if (editorService != null)
            {
                m_mask = mask;
                checkBox0.CheckState = (m_mask & (1 << 0)) == 0 ? CheckState.Unchecked : CheckState.Checked;
                checkBox1.CheckState = (m_mask & (1 << 1)) == 0 ? CheckState.Unchecked : CheckState.Checked;
                checkBox2.CheckState = (m_mask & (1 << 2)) == 0 ? CheckState.Unchecked : CheckState.Checked;
                editorService.DropDownControl(this);
            }

            return m_mask;
        }
        private UInt32 m_mask = 0;

}

ModifyMask(...) is the implemented function of IIntegerMaskControl interface which is being called from another class:

public interface IIntegerMaskControl
{
    UInt32 ModifyMask(IServiceProvider provider, UInt32 mask);
}

public class IntegerMaskEditor : UITypeEditor
{
    public static IIntegerMaskControl control = null;

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.DropDown;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if (control == null)
            return "Error: IIntegerMaskControl not set!";

        return control.ModifyMask(provider, (UInt32)value);
    }
}

And here is the property itself:

    [System.ComponentModel.CategoryAttribute("Base")]
    [Editor(typeof(IntegerMaskEditor), typeof(UITypeEditor))]
    public UInt32                                   renderMask { get; set; }

It works, but I got my control shown in White colors (including buttons) which just looks wrong. I cannot find out why. Here is the link to the screenshot: that is how the control looks like in action. Anyone have any ideas on why and how to avoid it? I could go with calling a Form but I'd rather stick with the dropdown.

Thanks in advance!

Was it helpful?

Solution

The property grid uses the ViewBackColor property to display the drop down back color. I don't see any other property that allows to change the drop down back color.

However, the control shown in the drop down is parented to a control (a Form) that you can modify, with a code like this:

public partial class MaskEditorControl : UserControl, IIntegerMaskControl
{
    private Color _initialBackColor;

    public MaskEditorControl()
    {
        InitializeComponent();
        _initialBackColor = BackColor;
    }

    protected override void OnParentChanged(EventArgs e)
    {
        base.OnParentChanged(e);
        if (Parent != null)
        {
            Parent.BackColor = _initialBackColor;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top