Henrique Henrique - 7 days ago 6
C# Question

How to add an existing control in another on properties panel at design time using C# WindowsFormApplication

I have custom controls:

CustomControlOne, CustomControlTwo
.

My
CustomControlOne
has a
List<CustomControlTwo>
showed in your properties panel of my windows form application project:

Properties Panel of CustomControlOne

So when I click in the button, a window to add new items in this collection is opened:

CustomControlTwo collection

But, I want add existing CustomControlTwo items that are defined in MyForm. ps.: MyForm contains CustomControlOne and multiple CustomControlTwo.

I want this items can be added in design time, like same way that selecting an item in a comboBox (in CustomControlOne properties panel). When I change
List<CustomControlTwo>
to
ICollection<CustomControlTwo>
a comboBox is showed but the items of type CustomControlTwo not appears :(

How I can do this ?
How I can say to my CustomControlOne where are the CustomControlTwo items ?
Thx.

Answer

If you want to add multiple CustomControlTwo items to your control, a drop down list will do you no good, since you can only select one value there.

If you are able to change the property to accept a single instance of CustomControlTwo (or are OK that the list you can create in your property browser has only one item), you can create a new System.ComponentModel.TypeConverter that will create the list to select from. A type converter is an abstract class and you will have to implement a couple of abstract methods. The methods that are valuable to you are GetStandardValuesSupported, GetStandardValuesExclusive and GetStandardValues. Here is the part that is interesting to you (you have to add the other methods):

public class CustomControlOneConverter : TypeConverter {
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        //By returning true, you tell the property designer to add a drop down list
        return true;
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        //By returning true, you tell the property designer to not allow the user to enter his own text
        return true;
    }


    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        //Get all objects of type CustomControlTwo from the container
        CustomControlTwo[] controlsToList =
            context.Container.Components.OfType<CustomControlTwo>().ToArray;

        //Return a collection of the controls
        return new StandardValuesCollection(controlsToList);
    }

    //implement the other abstract methods
}

What you do in GetStandardValues is to search in the container of CustomControlOne, if there are instances of CustomControlTwo. Those are then added to the list of standard values to select from in the property browser.

If you are not able to change your property to select only one CustomControlTwo, you will need to create your own UI to display when the ellipsis (...) is clicked in your property window. For that you need to create your own UITypeEditor. That, however is a more complex undertaking and is not easily explained in a nutshell.

Comments