How to get properties of a control

J

John

Hi

I am using the below code to get properties and then sub properties on an
object;

Dim Prop As PropertyDescriptor
Dim Props As PropertyDescriptorCollection

Props = TypeDescriptor.GetProperties(Ctrl)

For Each Prop In Props
Dim PropObject As Object = Prop.GetValue(Ctrl)
Dim converter As TypeConverter = TypeDescriptor.GetConverter(PropObject)
...
Next

Unfortunately the PropObject comes out as nothing for Ctrl =
{System.Windows.Forms.BindingNavigator}on line Dim PropObject As Object =
Prop.GetValue(Ctrl) and this gives 'System.ArgumentException was unhandled'
error on the line Dim converter As TypeConverter =
TypeDescriptor.GetConverter(PropObject). How can I get round this problem?
The value of prop when this error came as per watch is given below.

Thanks

Regards



- Prop {System.ComponentModel.ReflectPropertyDescriptor}
System.ComponentModel.PropertyDescriptor
+ System.ComponentModel.ReflectPropertyDescriptor
{System.ComponentModel.ReflectPropertyDescriptor}
System.ComponentModel.ReflectPropertyDescriptor
+ Attributes {System.ComponentModel.AttributeCollection}
System.ComponentModel.AttributeCollection
Category "Behavior" String
+ ComponentType {Name = "Control" FullName =
"System.Windows.Forms.Control"} System.Type
+ Converter {System.ComponentModel.ComponentConverter}
System.ComponentModel.TypeConverter
Description "The shortcut menu to display when the user right-clicks the
control." String
DesignTimeOnly False Boolean
DisplayName "ContextMenuStrip" String
IsBrowsable True Boolean
IsLocalizable False Boolean
IsReadOnly False Boolean
Name "ContextMenuStrip" String
+ PropertyType {Name = "ContextMenuStrip" FullName =
"System.Windows.Forms.ContextMenuStrip"} System.Type
SerializationVisibility Visible {1}
System.ComponentModel.DesignerSerializationVisibility
SupportsChangeEvents True Boolean
 
M

Marc Gravell

Actually, for a converter you should really ask the property itself
(since it can be set against a property using the
TypeConverterAttribute); as such, you should be using (if I can
remember VB syntax):

Dim converter As TypeConverter = Prop.Converter;

If no TypeConverterAttribute is set (for Prop), then this will
automatically use Prop.PropertyType to get a converter for the type
(rather than the instance). In short: it should work, even for null
values.

Marc
 

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