Question

I asked here how to use custom color dialog in property grid for Color struct:

Using custom color picker dialog in PropertyGrid

From that link you can see code of MyColorEditor class if needed. Now i can use custom color dialog but only if i use my own struct which is RGBA in that example.

If i use this custom type editor on color struct it looks like this in property grid:

screenshot1

But if i use RGBA struct which i created, it looks properly:

screenshot2

Problem happens because UITypeEditorEditStyle.Modal not applying with GetEditStyle() i think.

Using Color struct can be better than using my custom color struct for me because then i can set DefaultValue for Color property without requiring to write my own type converter.

So my question is how to use custom editor on Color struct.

Was it helpful?

Solution

Finally figured out how to do it, ColorConverter was causing this problem so need to use my own ColorConverter like this:

public class MyColorConverter : ColorConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return false;
    }
}

Editor codes:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace HelpersLib
{
    public class MyColorEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

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

            IWindowsFormsEditorService svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (svc != null)
            {
                Color color = (Color)value;

                using (DialogColor form = new DialogColor(color))
                {
                    if (svc.ShowDialog(form) == DialogResult.OK)
                    {
                        return (Color)form.NewColor;
                    }
                }
            }

            return value;
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
            Graphics g = e.Graphics;
            Color color = (Color)e.Value;

            if (color.A < 255)
            {
                using (Image checker = ImageHelpers.CreateCheckers(e.Bounds.Width / 2, e.Bounds.Height / 2, Color.LightGray, Color.White))
                {
                    g.DrawImage(checker, e.Bounds);
                }
            }

            using (SolidBrush brush = new SolidBrush(color))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }

            e.Graphics.DrawRectangleProper(Pens.Black, e.Bounds);
        }
    }
}

And example usage:

    [DefaultValue(typeof(Color), "Black"),
    Editor(typeof(MyColorEditor), typeof(UITypeEditor)),
    TypeConverter(typeof(MyColorConverter))]
    public Color Color { get; set; }

Screenshot:

screenshot

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top