locked
Custom Property for a Usercontrol RRS feed

  • Question

  • Hello,

    I have created a custom control having some text boxes, comboboxes and a list box. Now i want to create a custom property for a listbox which i can set at the design time when I use this custom control while designing GUI for some application.

    The problem is I want to have a property in 'Properties window' which gives me option like a checkbox list, in which I can select multiple values as per my requirements depending upon the application.

    Suppose my listbox has a property called "country" and I have three values for it like 'India, China, America' then I should get a option during design time to select any one of them to display in the list box.

    For example: If I select India and America in the 'country' field in the properties window, In the runtime the application should display only India and America in the listbox.

    Please help me to achieve this.

    Thanks & regards,

    Siddharth

    • Moved by CoolDadTx Monday, May 19, 2014 1:56 PM Winforms related
    Monday, May 19, 2014 5:37 AM

Answers

  • Hi, you can create a custom editor for it, here is an example:

        public class YouCustomControl
        {
            [Editor(typeof(CheckedListBoxUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))]
            public bool[] CountryVisible//set visible of your listbox item here
            {
                get;
                set;
            }
        }
        public class CheckedListBoxUITypeEditor : System.Drawing.Design.UITypeEditor
        {
            public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
            {
                return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
            }
    
            public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                var checkedListBox = new CheckedListBox();
                checkedListBox.Items.Add("India");
                checkedListBox.Items.Add("China");
                checkedListBox.Items.Add("America");
    
                var es = provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService)) as System.Windows.Forms.Design.IWindowsFormsEditorService;
                if (es != null)
                {
                    var data = value as bool[];
                    if (data != null)
                    {
                        for (int i = 0; i < Math.Min(data.Length, checkedListBox.Items.Count); i++)
                        {
                            checkedListBox.SetItemChecked(i, data[i]);
                        }
                    }
                    es.DropDownControl(checkedListBox);
                }
    
                var result = new bool[3];
                for (int i = 0; i < checkedListBox.Items.Count; i++)
                {
                    result[i] = checkedListBox.GetItemChecked(i);
                }
                return result;
            }
        }

    In property window, you will see:


    Monday, May 19, 2014 9:26 AM

All replies

  • Hi, you can create a custom editor for it, here is an example:

        public class YouCustomControl
        {
            [Editor(typeof(CheckedListBoxUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))]
            public bool[] CountryVisible//set visible of your listbox item here
            {
                get;
                set;
            }
        }
        public class CheckedListBoxUITypeEditor : System.Drawing.Design.UITypeEditor
        {
            public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
            {
                return System.Drawing.Design.UITypeEditorEditStyle.DropDown;
            }
    
            public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                var checkedListBox = new CheckedListBox();
                checkedListBox.Items.Add("India");
                checkedListBox.Items.Add("China");
                checkedListBox.Items.Add("America");
    
                var es = provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService)) as System.Windows.Forms.Design.IWindowsFormsEditorService;
                if (es != null)
                {
                    var data = value as bool[];
                    if (data != null)
                    {
                        for (int i = 0; i < Math.Min(data.Length, checkedListBox.Items.Count); i++)
                        {
                            checkedListBox.SetItemChecked(i, data[i]);
                        }
                    }
                    es.DropDownControl(checkedListBox);
                }
    
                var result = new bool[3];
                for (int i = 0; i < checkedListBox.Items.Count; i++)
                {
                    result[i] = checkedListBox.GetItemChecked(i);
                }
                return result;
            }
        }

    In property window, you will see:


    Monday, May 19, 2014 9:26 AM
  • Hi,

    Thanks for the reply. It was bit helpful but still I am not able to get the output. Please can u give any example of what has to be written in the below code:

    public class YouCustomControl
       
    {
          
    [Editor(typeof(CheckedListBoxUITypeEditor), typeof(System.Drawing.Design.UITypeEditor))]
           
    public bool[] CountryVisible//set visible of your listbox item here
           
    {
               
    get; What should be wrrite here to populate list box
               
    set; What should be wrrite here to populate list box
           
    }
       
    }

    Monday, May 19, 2014 11:39 AM