Reflection question : finding the exposed properties of a Control

J

JezB

Given a generic Control object, how can I iterate through all the properties
exposed by the control ? Different classes of control expose different
properties.

Ideally I'd like to find all the properties that are exposed within the
"Appearance" and "Layout" categories of the form designer's property grid.

I imagine I can do this via reflection but I don't really know where to
start.
Any pointers ?
 
B

Bob Powell [MVP]

You can use the TypeDescriptor.GetProperties object to obtain a list of
properties. Along with this you can also specify an array of attributes that
should be taken into consideration. This is how the PropertyGrid obtains
only those properties having the Browsable(True) attribute set.

In the array of attributes you should include a CategoryAttribute with the
name "Appearence" if you want the properties in that section.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
J

JezB

Thanks, I see what you mean but can't get the syntax quite right. How do you
specify the array of attributes and restrict the CategoryAttribute to
"Appearance" and "Layout" ? I can't find an example.
 
B

Bob Powell [MVP]

The array of attributes defined must all exist on a given property so you
may use a combination of Appearance and Browsable but not a combination of
Appearance and Layout.

The code after my signature demonstrates...


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

PropertyDescriptorCollection
pdc=TypeDescriptor.GetProperties(typeof(Control),new Attribute[]{new
CategoryAttribute("Appearance"),new BrowsableAttribute(true)});

foreach(PropertyDescriptor pd in pdc)

System.Diagnostics.Trace.WriteLine(pd.Name);

pdc=TypeDescriptor.GetProperties(typeof(Control),new Attribute[]{new
CategoryAttribute("Layout"),new BrowsableAttribute(true)});

foreach(PropertyDescriptor pd in pdc)

System.Diagnostics.Trace.WriteLine(pd.Name);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top