Asaf Shazar
Asaf Shazar

Reputation: 1065

How to add a custom property to a UserControl which display as a combo box at the design time

Edit: The question is about adding a property during the design time, not during the runtime.

I created a UserControl, in which contains a button, and I want to add a new property that when user uses it in form, he could see the property FillColor in user control's Property Tab. What's more, that property should be in the form of a combobox, which allows user to select the Color in System.Drawing.Color.

For Example:

I call my UserControl HalfFill. What it does is to fill a normal button from start to the half of he size.

Now I want 2 properties to be customized using Property Tab:

And when user chooses the color , the choices are listed in combobox (behaves like BackColor property for normal buttons), and all of them come from System.Drawing.Color.

I want to do it with Enum. Can someone help me? How i put the comboxBox with its values into the Properties Tab?

Here is an example that I am looking for

public partial class HalfButton: UserControl
{
    public ComboBox ChooseColor //of all color in System.Drawing.Color
    {
        /* must be in that comboBox all the color in Color library so the user could choose 
        from properies of the button the color he want */

        get { return x.Item[colorfill.ToString()); }
        set { colorfill = Color.FromName(value); OnPaint(null); }
    }

    private Color colorfill = Color.Tomato;

    ComboBox x;

    public HalfButton()
    {
        InitializeComponent();

        x = new ComboBox();
        ArrayList ColorList = new ArrayList();
        Type colorType = typeof(System.Drawing.Color);
        PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static |
                                                              BindingFlags.DeclaredOnly |
                                                              BindingFlags.Public);
        foreach (PropertyInfo c in propInfoList)
        {
            x.Items.Add(c.Name);
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        MessageBox.Show(colorfill+""); 
    }
}

Upvotes: 1

Views: 2145

Answers (3)

Asaf Shazar
Asaf Shazar

Reputation: 1065

That my last version... now i only need to bind the comboBox values (that in the combBox) to the properties when i try that it show

ChooseColor1 | None

    public static ComboBox ComboFill()
             {
                 ComboBox xx = new ComboBox();
                 ArrayList ColorList = new ArrayList();
                 Type colorType = typeof(System.Drawing.Color);
                 PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
                 foreach (PropertyInfo c in propInfoList)
                 {
                     xx.Items.Add(c.Name);
                 }
                 return xx;
             }
           private Color colorfill1 = Color.Tomato;
           private Color colorfill2 = Color.Tomato;
           private ComboBox x1=ComboFill();
           private ComboBox x2=ComboFill();

     public ComboBox ChooseColor1 {

            get { return x1; }
            set { x1=value; OnPaint(null); }

        }
     public ComboBox ChooseColor2 {

            get { return x2; }
            set { x2=value; OnPaint(null); }

        }

     protected override void OnPaint(PaintEventArgs e)
            {
                    //my functions
//something with colorfill1
//something with colorfill2


            }

Upvotes: 1

nevets
nevets

Reputation: 4818

Do you mean the property in the Designer? Please take a look at my example below.

I created a UserControl called ColorTextBox, and defined a property called FilledColor. In the designer, I am able to change the color in a combo box:

The property is shown in the Property Tab:

The property is shown in the Property Tab

Colors in the combobox, Drawing.Colors are under Web tab:

Colors in the ComboBox

After change the FilledColor to Highlight:

after change

My code is fairly simple:

private Color _filledColor;

public ColorTextBox()
{
    InitializeComponent();
    _filledColor = Color.FromKnownColor(KnownColor.Control);
}

public Color FilledColor
{
    get { return _filledColor; }
    set
    {
        _filledColor = value;
        button1.BackColor = _filledColor;
    }
}

The only change is to change your property type to Color, and the magic completed.

Upvotes: 1

Seminda
Seminda

Reputation: 1773

following code will give you an idea how you can bind the combo using System.Drawing.Color. Also need to use namespace using System.Reflection;

ArrayList ColorList = new ArrayList();
        Type colorType = typeof(System.Drawing.Color);
        PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
        foreach (PropertyInfo c in propInfoList)
        {
            this.comboBox1.Items.Add(c.Name);
        }

Upvotes: 1

Related Questions