views:

46

answers:

1

Using WinForms I am coding a Component user control where I would like it to be able to display a designer editor form where I could see the list of all the controls names on the form where the component is located.

I got the basic mechanics of the component user control and the support of the designer editor. My question is not about that.

Let's say I have a form where, in the IDE, I place the component in the component tray and several other controls on the form. Now, I invoke the designer editor for the component and I display the list of controls names.

Here's a snippet of code:

internal class MyComponentEditor : UITypeEditor
{
    public override object EditValue
    (
        ITypeDescriptorContext context,
        IServiceProvider provider, object value
    )
    {
        var instance = context.Instance as MyComponent;
        var container = instance.Container;
        var controls = container.Components.Cast<Control>;
        var names = controls.Select (x => x.Name);
    }
}

Here, the names variable should contain the names of all the controls but I only get the type name!

Must be something obvious... ;)

+1  A: 

Ok, I've nailed it.

You need to do the following to get the name of the control, for each control:

(string)TypeDescriptor.GetProperties (control)["Name"].GetValue (control)

From my example:

var names = controls.Select (x => (string)TypeDescriptor
                                      .GetProperties(x)["Name"]
                                      .GetValue(x));
Stecy