Loop ALL Properties

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

I am going in circles trying to loop through properties on 3rd party
controls. For example, I have a textbox that has its maximum length located
at MyTextBox.Properties.MaxLength - instead of the dotnet textbox which is
MyTextBox.MaxLength. If I loop a built in dotnet control, it finds the
property no problem. But looping through the 3rd party control,
Properties.MaxLength does not get listed. I was hoping to find how it names
it using reflection, so I could use GetValue and SetValue to dyanmically set
values of controls without knowing their type until runtime. How can I gain
access to loop ALL the properties of a control?

Dim t As Type = ctl.GetType
For Each pp As PropertyInfo In t.GetProperties()
Console.WriteLine("{0} = {1}", pp.Name, pp.GetValue(ctl, Nothing))
Next
 
Hi Derek,

Did I get it right that MyTextBox.Properties is sort of your own array of
'properties', like dictionary? If so, that how it relates to 'real properties',
'native' for a class?

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
 
3rd Party Developer Express textbox control. It has many properties. Can't
figure out how to loop and get all of them.

Alex Meleta said:
Hi Derek,

Did I get it right that MyTextBox.Properties is sort of your own array of
'properties', like dictionary? If so, that how it relates to 'real
properties', 'native' for a class?

Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com
 
This is taken straight from
http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx.
It's not actually recursive - you have to repeat it for each member type of
interest.

I created a picture box with some scroll bars, which is similar (I think) to
your custom controls, and sent the output to a multiline text box.

Imports System.Reflection
Public Class Form1
Dim indent As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim t As Type = PictureBox1.GetType
For Each pp As PropertyInfo In t.GetProperties()
listproperties(pp)
Next
End Sub
Sub listproperties(ByVal pp As PropertyInfo)
Display(indent, pp.Name)
indent += 1
For Each mi As MethodInfo In pp.GetAccessors
ListAccessors(mi)
Next
indent -= 1
End Sub
Sub ListAccessors(ByVal mi As MethodInfo)
Display(indent, mi.Name)
End Sub
Sub Display(ByVal indent As Int32, ByVal s As String)
textBox1.Text += New String(" "c, indent * 2)
textBox1.Text += s & vbCrLf
End Sub
End Class
 
More on the right track, but I cannot seem to get to all the properties. In
the dev express controls, on a textboxedit control, in the property sheet,
there is a property called Properties. I expand that and see a MaxLength
property. So the property would be textbox1.properties.MaxLength - cannot
seem to loop through and get the names of all the properties, so I can find
how this one is named, and use it at runtime to set values to it, because I
will not know the type of control until runtime. And I don't want to convert
the control because there are too many controls to do this for. So if the
property exists, I want to do a setvalue on it. Any ideas on how to find all
properties?
 
I don't think that .Properties and .Properties.MaxLength can both be
properties. How are these members defined within the class?
 
I don't think that .Properties and .Properties.MaxLength can both be
properties.

Why not? The Properties property probably just returns a class, which itself
has properties.
 
That's why I believe recursion should work. But I can't see a way to create
a propertyinfo from a property type. If I could do that, then it would solve
the problem. Perhaps I should have said "Reflection apparently doesn't
support both Properties and Properties.Maxlength as properties of the same
object" (which is the way that the user thinks of them). Some additional
process is required to get access to the properties of a property.
 
Hi!

I also use in my apps the dx controls. I do in my loop over all controls a
simple "select case" to difference beetween the differnt controltypes and
then i set the properties.

That works pretty good.


HTH Wolfgang
 
Back
Top