From usercontrol code in design mode, how do I get all the names of the controls contained in the parent?

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

I got the basic mechanics of the user component element and the support of the designer editor. This is not my question.

Let's say I have a form where in the IDE I put the component in the component tray and several other controls on the form. Now I call the constructor editor for the component, and I show a list of the names of the controls.

Here is the code snippet:

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 get only the type name!

There must be something obvious ...;)

+4
source share
1 answer

Ok, I nailed it.

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

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

In my example:

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

All Articles