Question

In PropertyGrid default color picker dialog not allow to set alpha value of color.

I already made my own color picker dialog and want to use it in PropertyGrid but not sure how to do it.

Was it helpful?

Solution

I managed to use my custom color picker dialog in property grid and copying code of it here in case some need it too:

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(RGBA))
            {
                return value;
            }

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

            if (svc != null)
            {
                using (DialogColor form = new DialogColor((RGBA)value))
                {
                    if (svc.ShowDialog(form) == DialogResult.OK)
                    {
                        return form.NewColor.RGBA;
                    }
                }
            }

            return value;
        }

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

        public override void PaintValue(PaintValueEventArgs e)
        {
            using (SolidBrush brush = new SolidBrush((RGBA)e.Value))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }

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

And this is how it looks in property grid:

screenshot1

When i click button of it, it will open custom color dialog.

But still have one problem which i can't solve. I can't use Color struct with this UITypeEditor, therefore created RGBA class. When i use color struct, it look like this:

screenshot2

I will open another question for it i guess: Custom ColorEditor does not work properly on Color struct

OTHER TIPS

To interact with PropertyGrid, you have to create your own "property class" (as described here). You can customise different parts and thus there are multiple solutions for what you want. As a first approach to your problem, here you have a code for propertyGrid1:

Property curProperty = new Property();
propertyGrid1.SelectedObject = curProperty;

Where Property is defined by:

public class Property
{
    private ColorDialog _dialog = new customColorDialogDialog();

    public ColorDialog dialog
    {
        get { return _dialog; }
        set { _dialog.ShowDialog(); }
    }
}
class customColorDialogDialog : ColorDialog
{

}

In this code, your color dialog (customColorDialogDialog) is triggered when clicking on the cell on the right hand side of the property name ("dialog").

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