By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,514 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

Loop ALL Properties

P: n/a
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

Oct 28 '08 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
i

Oct 28 '08 #2

P: n/a
You should be looping recursively.

"Derek Hart" <de********@yahoo.comwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
>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


Oct 28 '08 #3

P: n/a
3rd Party Developer Express textbox control. It has many properties. Can't
figure out how to loop and get all of them.

"Alex Meleta" <am*****@gmail.comwrote in message
news:df*************************@news.microsoft.co m...
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
>i


Oct 28 '08 #4

P: n/a
Do you have a sample of how I would do that from my code below?

"James Hahn" <jh***@yahoo.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
You should be looping recursively.

"Derek Hart" <de********@yahoo.comwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
>>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



Oct 28 '08 #5

P: n/a
This is taken straight from
http://msdn.microsoft.com/en-us/libr...pertyinfo.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

"Derek Hart" <de********@yahoo.comwrote in message
news:ud**************@TK2MSFTNGP04.phx.gbl...
Do you have a sample of how I would do that from my code below?

"James Hahn" <jh***@yahoo.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>You should be looping recursively.

"Derek Hart" <de********@yahoo.comwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
>>>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



Oct 28 '08 #6

P: n/a
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?

"James Hahn" <jh***@yahoo.comwrote in message
news:O3**************@TK2MSFTNGP03.phx.gbl...
This is taken straight from
http://msdn.microsoft.com/en-us/libr...pertyinfo.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

"Derek Hart" <de********@yahoo.comwrote in message
news:ud**************@TK2MSFTNGP04.phx.gbl...
>Do you have a sample of how I would do that from my code below?

"James Hahn" <jh***@yahoo.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>>You should be looping recursively.

"Derek Hart" <de********@yahoo.comwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
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



Oct 28 '08 #7

P: n/a
I don't think that .Properties and .Properties.MaxLength can both be
properties. How are these members defined within the class?

"Derek Hart" <de********@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
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?

"James Hahn" <jh***@yahoo.comwrote in message
news:O3**************@TK2MSFTNGP03.phx.gbl...
>This is taken straight from
http://msdn.microsoft.com/en-us/libr...pertyinfo.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

"Derek Hart" <de********@yahoo.comwrote in message
news:ud**************@TK2MSFTNGP04.phx.gbl...
>>Do you have a sample of how I would do that from my code below?

"James Hahn" <jh***@yahoo.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
You should be looping recursively.

"Derek Hart" <de********@yahoo.comwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
>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
>
>
>
>
>

Oct 28 '08 #8

P: n/a
"James Hahn" <jh***@yahoo.comwrote in message
news:uo**************@TK2MSFTNGP06.phx.gbl...
>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.
Oct 28 '08 #9

P: n/a
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.

"Jeff Johnson" <i.***@enough.spamwrote in message
news:ev**************@TK2MSFTNGP02.phx.gbl...
"James Hahn" <jh***@yahoo.comwrote in message
news:uo**************@TK2MSFTNGP06.phx.gbl...
>>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.
Oct 28 '08 #10

P: n/a
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
"Derek Hart" <de********@yahoo.comschrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP05.phx.gbl...
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?

"James Hahn" <jh***@yahoo.comwrote in message
news:O3**************@TK2MSFTNGP03.phx.gbl...
>This is taken straight from
http://msdn.microsoft.com/en-us/libr...pertyinfo.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

"Derek Hart" <de********@yahoo.comwrote in message
news:ud**************@TK2MSFTNGP04.phx.gbl...
>>Do you have a sample of how I would do that from my code below?

"James Hahn" <jh***@yahoo.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
You should be looping recursively.

"Derek Hart" <de********@yahoo.comwrote in message
news:eI**************@TK2MSFTNGP03.phx.gbl...
>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
>
>
>
>
>

Oct 29 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.