Click here to Skip to main content
16,010,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have made a user control for which i want to have a custom property in its "Properties" window.

There are two requirements to be fullfilled :

1. Property should show a check list box so that user can select the desired values at the design time.

2. The values for this property should come from - how many similar user controls are present on the form.

for example : If i insert one usercontrol on the form (i.e usercontrol_1), then I add two more instance of similar usercontrols(i.e usercontrol_2 & usercontrol_3) on the same form.

Now when I set the custom property of usercontrol1, it should have a dropdown checklistbox with the values usercontrol_2 & usercontrol_3. So in short, I want to link one user control with another similar usercontrol.

Note:
the checklistbox of the property should automatically populate as soon as I add new usercontrol on the form.

Please help if someone have solution for this.

Thanks & regards,
Siddharth
Posted
Comments
Maciej Los 22-May-14 12:50pm    
What have you done till now?
Where are you stuck?
Sid_ScienceKid 23-May-14 5:51am    
Hi Los,

I have updated the latest solution. Please try it in your own usercontrol with 1 Listbox and 1 Label. An then check out the "NoofUserListOnForm" property of this usercontrol by placing it in a new form in new project.

It might give you a better idea what I want to achieve.

1 solution

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Design;
using System.Windows.Forms.Design;

namespace WindowsFormsApplication12
{
    public partial class UserList : UserControl //Made one usercontrol name userlist.
    {
        public UserList()
        {
            InitializeComponent();
        }

        Control ParentForm; // declared one control
       List<Control> objControlList = new List<Control>();
       

       private void UserList_Load(object sender, EventArgs e)
       {
           ParentForm = this.Parent; // Parent form equals to the form or container on which usercontrol is placed.
           GetAllControls(ParentForm); // Method declared below: Gives list of all 'userlist' usercontrol present on the Parentform
           UserControlEditor.PropertyCopyControlList = objControlList;
           label1.Text = Convert.ToString(UserControlEditor.PropertyCopyControlList.Count);
       }


        private void GetAllControls(Control container) //Gives list of all 'userlist' usercontrol present on the Parentform
       {
           foreach (Control c in container.Controls)
           {
               GetAllControls(c);
               if (c is UserList) objControlList.Add(c);
           }
       }

        bool[] objbool = new bool[5];
       [Description("Items in the lisbox"), Editor(typeof(UserControlEditor), typeof(UITypeEditor))]
        public bool[] NoOfUserlistsOnForm // Made a custom property to have a checklist box in Properties window to set value for this property .
        {
            get 
            {
                return objbool;
            }
            set 
            {
                objbool = value;
                {
                    for (int i = 0; i < (objControlList.Count); i++)
                   {
                        if (objbool[i] != false)
                        {
                            listBox1.Items.Add(objControlList[i].Name);
                        }
                    }
                }
            }
        }
 
    }

//--------------------------------------------------------------------------------------------------------------------------------------------------------


    class UserControlEditor : UITypeEditor // Declared a class to have a checklist box in properties window of usercontrol
    {        
        public UserControlEditor() 
        {       
            
        }

        public static List<Control> CopyControlList = new List<Control>();
        public static List<Control> PropertyCopyControlList
        {
            get { return CopyControlList; }
            set { CopyControlList = value; }
        }

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

        
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            //use IWindowsFormsEditorService object to display a control in the dropdown area
            IWindowsFormsEditorService frmsvr = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (frmsvr == null)
                return null;
            CheckedListBox Clbox = new CheckedListBox();
            //Here I have to populate the checklistbox with the code - Clbox.Items.Add(objectName)
            // But how get the ControlList (which is declared in the userlist class) to this class(UserControlEditor).
            for (int i = 0; i < PropertyCopyControlList.Count; i++)
            {
                Clbox.Items.Add(PropertyCopyControlList[i].Name);
            }

            var es = provider.GetService(typeof(System.Windows.Forms.Design.IWindowsFormsEditorService)) as System.Windows.Forms.Design.IWindowsFormsEditorService;

            es.DropDownControl(Clbox);

            if (es != null)
            {
                var data = value as bool[];
                if (data != null)
                {
                    for (int i = 0; i < Math.Min(data.Length, Clbox.Items.Count); i++)
                    {
                        Clbox.SetItemChecked(i, data[i]);
                    }
                }
                es.DropDownControl(Clbox);
            }

            var result = new bool[5];
            for (int i = 0; i < Clbox.Items.Count; i++)
            {
                result[i] = Clbox.GetItemChecked(i);
            }
            return result;
        }
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900