PropertyGrid - run through all of its properties

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

When PropertyGrid is connected to a selectedObject :
How can I run through all of the object properties, which are seen on the
propertyGrid, and get the name + value ?
(What is behind PropertyGrid ?)

Thanks :)
 
Am 20.06.2010 21:23, schrieb Mr. X.:
When PropertyGrid is connected to a selectedObject :
How can I run through all of the object properties, which are seen on the
propertyGrid, and get the name + value ?
(What is behind PropertyGrid ?)

What is the problem with the solution you already have? Doesn't
it work?
 
No.
(I have answered the previous thread).
As your advise, I did getProperties & getCustomAttributes either :
prs = ... getProperties
for I = 0 to prs.count - 1
dim prop = prs(j)
dim atts = prop.GetCustomAttributes
for each att in atts ... ' as your code below.

.... but I have reach the inner loop only when :specialAtt.Visibility was
hidden.
(And I don't get all of the properties, that are seen on the designer :
properties view).

It doesn't meter whether I use a serializable object or using Reflection,
but, I see that serializable isn't possible for Panel, I.e, and I didn't
understand reflection (I don't know if I should use getProperties method, or
getFields method -
both don't return the same fields' count as the property grid does on design
time).

Thanks :)
 
Let's a look of PictureBox, for example.
PictureBox has 28 properties, that are seen on design-time.
When I do
dim myPictureBox as PictureBox

myPictureBox.GetType().GetProperties().count ' count = 81
myPictureBox.GetType().GetFields().count ' count = 0

So I filter (by loop) : prs = myPictureBox.GetType().GetProperties()
j = 0
for I = 0 to prs.count - 1
if prs(I).canRead andAlso prs(I).canWrite andAlso
prs(I).PropertyType.TypeInitializer Is Nothing then
j = j + 1
end if
next

.... now k = 25 (almost, but the properties don't match the original
properties.
Here is the new list :
BorderStyle
ErrorImage
Font
Image
InitialImage
ImeMode
RightToLeft
SizeMode
TabIndex
AccessibleRole
Anchor
BackgroundImage
BackgroundImageLayout
BindingContext
ContextMenu
ContextMenuStrip
Dock
Height
Left
Region
Site
Tag
Top
Width
WindowTarget
-----------

There must be somewhere an example arround the internet, but I cannot find
one.
Need simple sample - how to retrieve & store, I.e., a Panel with all of its
properties, please.
Thanks :)
 
Also - propertyGrid may be a good solution, if I knew how to resolve by
propertyGrid all of the properties of selected control.
 
Am 20.06.2010 22:20, schrieb Mr. X.:
No.
(I have answered the previous thread).
As your advise, I did getProperties & getCustomAttributes either :
prs = ... getProperties
for I = 0 to prs.count - 1
dim prop = prs(j)
dim atts = prop.GetCustomAttributes
for each att in atts ... ' as your code below.

.... but I have reach the inner loop only when :specialAtt.Visibility was
hidden.
(And I don't get all of the properties, that are seen on the designer :
properties view).

It doesn't meter whether I use a serializable object or using Reflection,
but, I see that serializable isn't possible for Panel, I.e, and I didn't
understand reflection (I don't know if I should use getProperties method, or
getFields method -
both don't return the same fields' count as the property grid does on design
time).

Your question in this thread is how to run through the properties of an object.
You can do it with reflection, and you know how to do it. I still don't understand
what you can not do with it.

A Panel is not serializable. You have to take it as it is, i.e. you have to
write your own code to write the property values to wherever you want.

'GetProperties' returns the properties and 'GetField' gets the fields. :-)
I think the names say it. As you are doing these kinds of tasks, I think you
already know the difference because these are basic terms in OOP:

http://msdn.microsoft.com/en-us/library/exe76ct6(VS.90).aspx

The VB documentation is sometimes more made for dummies (or "beginners") than structrued
very well. Probably therefore, fields are described as part of the property explanation:

http://msdn.microsoft.com/en-us/library/8yx6f707(VS.90).aspx
 
Am 20.06.2010 22:36, schrieb Mr. X.:
Also - propertyGrid may be a good solution, if I knew how to resolve by
propertyGrid all of the properties of selected control.

I can also only look in the documentation of the PropertyGrid
to find out which properties are shown by the property grid.
It says that public properites that don't have the BrowsableAttribute(False)
attached, are displayed.


Don't forget that properties can be inherited or not, can have
different access modifiers (public, private, etc) and they can be
static or instance members. You can pass the appropriate System.Reflection.BindingFlags
value(s) to the GetProperties method to get only the required
properties.


The code below returns 28 properties. I don't know if it's the 28 you've mentioned
in the other reply:

Dim t = GetType(PictureBox)
Dim tBrowsable = GetType(System.ComponentModel.BrowsableAttribute)

For Each prop In t.GetProperties(Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance)
Dim atts = prop.GetCustomAttributes(tBrowsable, False)
If atts.Length = 0 Then
Debug.Print(prop.Name)
End If
Next
 
Great.
Now I just have to check this.
(Where did you find that documentation ? I want to search this code either).
How can I check this (checking the public and attribute. Is my code correct
?)
Still there are some problems.
Specifically for panel, the properties: UseWaitCursor, autoSize,
autoSizeMode have the attribute : browsableAttribute, and are shown on
design time.
Also there are extra properties, that logically are considered : Site,
displayRectangle.

As the following code:
=================
prs =
myControl.GetType().GetProperties(BindingFlags.Public Or
BindingFlags.Instance)
k = 0
foundBA = False ' flag for finding the attribute
For j = 0 To prs.Count - 1
foundBA = False ' flag for finding the attribute
For Each att In prs(j).GetCustomAttributes(False)
If TypeOf att Is
System.ComponentModel.BrowsableAttribute Then
foundBA = True
End If
Next
If Not foundBA Then
k += 1
end if
next
msgBox ("k = " & k) ' now k = 34, where it should be 35
!!!
 
Am 21.06.2010 00:04, schrieb Mr. X.:
Great.
Now I just have to check this.
(Where did you find that documentation ? I want to search this code either).
How can I check this (checking the public and attribute. Is my code correct
?)

"Remarks" section, 5th paragraph:
http://msdn.microsoft.com/en-us/library/system.windows.forms.propertygrid(VS.90).aspx
Still there are some problems.
Specifically for panel, the properties: UseWaitCursor, autoSize,
autoSizeMode have the attribute : browsableAttribute, and are shown on
design time.

My fault. I did not code what I said. :-) Only checking whether the BrowsableAttribute
is not attached is not sufficient. The property is also included if the attribute's
Browsable is True:

If atts.Length = 0 _
OrElse DirectCast(atts(0), System.ComponentModel.BrowsableAttribute).Browsable Then

Debug.Print(prop.Name)
End If

This results in 37 properties.

Also there are extra properties, that logically are considered : Site,
displayRectangle.
[...]
msgBox ("k = " & k) ' now k = 34, where it should be 35

If 35 + 2 (Site + displayRectangle) = 37 = correct then it should be the right result now.
 
I don't know if site & displayRectangle always occurs in properties, when
their BrowsableAttribute is Browsable=true.
What make those property unique, so I can check not only by their name, and
delete them from my list, but by some attributes, or something else ?

Thanks :)

Armin Zingler said:
Am 21.06.2010 00:04, schrieb Mr. X.:
Great.
Now I just have to check this.
(Where did you find that documentation ? I want to search this code
either).
How can I check this (checking the public and attribute. Is my code
correct
?)

"Remarks" section, 5th paragraph:
http://msdn.microsoft.com/en-us/library/system.windows.forms.propertygrid(VS.90).aspx
Still there are some problems.
Specifically for panel, the properties: UseWaitCursor, autoSize,
autoSizeMode have the attribute : browsableAttribute, and are shown on
design time.

My fault. I did not code what I said. :-) Only checking whether the
BrowsableAttribute
is not attached is not sufficient. The property is also included if the
attribute's
Browsable is True:

If atts.Length = 0 _
OrElse DirectCast(atts(0),
System.ComponentModel.BrowsableAttribute).Browsable Then

Debug.Print(prop.Name)
End If

This results in 37 properties.

Also there are extra properties, that logically are considered : Site,
displayRectangle.
[...]
msgBox ("k = " & k) ' now k = 34, where it should be
35

If 35 + 2 (Site + displayRectangle) = 37 = correct then it should be the
right result now.
 
Am 21.06.2010 10:46, schrieb Mr. X.:
I don't know if site & displayRectangle always occurs in properties, when
their BrowsableAttribute is Browsable=true.
What make those property unique, so I can check not only by their name, and
delete them from my list, but by some attributes, or something else ?

I don't know what makes them unique or why you think they are unique. They are
properties like others and browsable=true. Therefore they are listed. You can
check all their attributes by calling GetCustomAttributes.
 
Armin,

I have seen endless times here Herfried replying about the browsable
attribute, and I've almost the same times written that it does not make a
property invisible in a property grid.

I also think it should do that, so I don't know what Net versions that were
and if it is currently doing what you would expect.

At least I was never able to make a public property invisible in the grid.
And then come forever with the sample background from a picturebox, which in
fact does nothing and would be better not in the property grid of the
designer.

Cor
 
Am 21.06.2010 12:23, schrieb Cor Ligthert[MVP]:
Armin,

I have seen endless times here Herfried replying about the browsable
attribute, and I've almost the same times written that it does not make a
property invisible in a property grid.

I also think it should do that, so I don't know what Net versions that were
and if it is currently doing what you would expect.

At least I was never able to make a public property invisible in the grid.
And then come forever with the sample background from a picturebox, which in
fact does nothing and would be better not in the property grid of the
designer.

I've never used the PpropertyGrid. :-D

I've only read the documentation and counted the # of properties in the
property window in the IDE if I select a picturebox or a panel in the Form
designer.

I don't challenge the whole task every time. ;)
 
Am 21.06.2010 12:19, schrieb Armin Zingler:
Am 21.06.2010 10:46, schrieb Mr. X.:

I don't know what makes them unique or why you think they are unique. They are
properties like others and browsable=true. Therefore they are listed. You can
check all their attributes by calling GetCustomAttributes.

I've only shown you the part you've asked for. I don't write the final code.
You should combine my information with what you already have. I guess you've
ignored 'CanWrite' this time. DisplayRectangle is readonly.
 
Back
Top