0

I'm looking to replicate some behavior that visual studio already does for me: have a dropdown, in the designer, allow me to select other components in the same forms.

Take the following example: A form and a button.

From the form, there is an 'OKButton' property that you can set. When you drop down the dialog, all of the possible Buttons appear as possible selections in the dropdown.

I have something like that, where I want a textbox to have property called "ServiceMember". When you expand that, it will allow me to choose from all the public members of my form that have the type "ServiceObject".

Is this possible to have this work in any automatic sense? And if not, I'm not sure how to populate the combobox with names that aren't in the current object. They're a member of the parent form?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
greggorob64
  • 2,487
  • 2
  • 27
  • 55

1 Answers1

0

If you're trying to do this as part of a designer (inferred from tags rather than anything in the question text) then any public property that you have on your custom control that has an AttributeProvider attribute that is of type IListSource should be represented in a property designer as a selection. i.e.:

[AttributeProvider(typeof(IListSource))]
public object MyList { get; set; }

A list selection is also automatically generated from an enum - see How can I add a combobox in control designer properties for a WinForms custom control? - but this is probably of no use for you in this case.

However the population of this list would be a bit of a pain - you can probably use Reflection to inspect the host control/form to look for public members that inherit from ServiceObject - e.g. see Check if a class is derived from a generic class or equally you can cast to the type and see if you get a non-null return:

ServiceObject potentialServiceObject = formMember as ServiceObject;
if (potentialServiceObject != null)
{
   // Add to list for dropdown
}
Community
  • 1
  • 1
kaj
  • 5,133
  • 2
  • 21
  • 18
  • Its usually bad practice to have the title duplicate the tags (I've seen many mini-flames on questions such as "How to do X in C#") but in your description of the problem - if it is about a custom control designer - then it would be helpful to clarify that. – kaj Feb 27 '12 at 19:06
  • Using reflection to look at the controls parent, then navigate through each control looking for the correct object types would definitely be a pain, but it may be my only option. – greggorob64 Feb 27 '12 at 19:06