Component Attributes in Properties Dialog

mafrosis

Well-known member
Joined
Jun 5, 2006
Messages
88
Location
UK
Programming Experience
5-10
Hey all, I have a question about designing component for the properties dialog - I understand how to mark a public attribute to be shown in the dialog but I want to achieve the parent-child treenode effect seen in some props. The location property is a good example:

location.jpg


Here is an example of the code I used for a BorderColor attribute on another component:

VB.NET:
<System.ComponentModel.DefaultValueAttribute(GetType(System.Drawing.Color), "WindowFrame"), _
    System.ComponentModel.CategoryAttribute("Appearance"), _
    System.ComponentModel.DescriptionAttribute("The border color used to paint the control.")> _
Public Property BorderColor() As System.Drawing.Color
    Get
        Return Me._BorderColour
    End Get
    Set(ByVal Value As System.Drawing.Color)
        Me._BorderColour = Value
        Me.Invalidate()
    End Set
End Property
edit:
Forgot to mention that I dont understand how one control can have multiple properties with the same name - it must come from the parent-child thing. For instance, a Panel object can have Width & Height for both AutoScrollMargin and AutoScrollMinSize (not to mention Size), can someone explain the syntax for this please?

Thanks for any assistance..

mafro (my first post woot!)
 
The attributes for properties has nothing really to do with how your property will be displayed in the properties window, however they do have everything to do with where your property will be displayed and how to serialize, convert etc..i.e category. The description attribute will obviously display what ever text you put in there at the bottom of the properties window when your property is selected.Browsable - determines whether or not it will be displayed in the properties window at all. Along with all the other attributes that you can use to aid the appearance, serialization of your property or the contents of your property.
The tree view thing you mentioned is due to the type of the property. For example. The location property you have shown in your post is of the type 'Point' which takes 2 arguments. Visual studio knows this type and uses the correct type converter to display the property as you see it there. Colors again, is a different type, but again VS also knows how to convert this type to and from a string that is why you will see the color dialog picker when you delcare a property of the type color.If you are creating your own property with a type that is unknown VS then you will need to 'explain' to VS how your type can be converted to and from a string by means of a custom type converter.
To answer your last question is the same as i have described above, the size type takes 2 argumentsa width and height, again VS knows this and correctly converts the values it is the same for autoscrollmargin etc...
 
Spot on mate, thanks for the swift reply..

Any suggestions where to find syntax for declaring a 'Point' type? Searching for help on this topic is difficult because the terms are so ambiguous - 'attribute', 'point', 'property' etc..

Thanks again
mafro
 
private _mypoint as point


Public Property MyPointType As Point
Get
Return _myPoint
End Get
Set (Byval value as point)
_mypoint = value
End set
End property
 
mafrosis said:
mafro (my first post woot!)
Welcome to the forums!
mafrosis said:
I want to achieve the parent-child treenode effect seen in some props.
It can be done, I will get back to that shortly once I have explained what you have misunderstood about the other properties.
mafrosis said:
The location property is a good example
Location is a property of system type Point, which has got X and Y integer values.
mafrosis said:
Forgot to mention that I dont understand how one control can have multiple properties with the same name - it must come from the parent-child thing. For instance, a Panel object can have Width & Height for both AutoScrollMargin and AutoScrollMinSize (not to mention Size), can someone explain the syntax for this please?
They don't have the same name. "AutoScrollMargin", "AutoScrollMinSize" and "Size" are different names. These are all properties of system type Size, which in turn has got Width and Height values.

Now you see that these are all single properties with unique names. The only parent-child relation found otherwise in properties panel is that of categories. You may have a single property of a user defined type that consist of several different values, for example a Name property of your own type NameClass that consist of three string type properties First name, Middle name and Last name. To make this display as one property with several sub-values like Location follow the guideline in the Code Project article "Using PropertyGrid Part-I" specifically the "Displaying custom data types with expandable properties" section, http://www.codeproject.com/vb/net/using_propertygrid.asp
 
Ok cool thats really quite obvious.. but it brings me back to my first question - how do you name the properties to anything other than X and Y?

I want to list my point properties as Right and Bottom:

padding.jpg


Here's my property code for good measure:

VB.NET:
<System.ComponentModel.CategoryAttribute("Layout"), _
    System.ComponentModel.DescriptionAttribute("The padding around tiled controls.")> _
Public Property Padding() As Point
    Get
        Return Me._Padding
    End Get
    Set(ByVal Value As Point)
        If Me.DesignMode Then
            Me._Padding = Value
            Me.Invalidate()
        End If
    End Set
End Property
Thanks a lot for this one!
mafro
 
JohnH said:
They don't have the same name. "AutoScrollMargin", "AutoScrollMinSize" and "Size" are different names. These are all properties of system type Size, which in turn has got Width and Height values.

Thanks John, thats exactly what I was looking for.. I understand now! Ill head off and read that tutorial.

And ill see if I can find a question from someone I can answer - have to keep the karma flowing! :eek:

Cheers
mafro
 
mafrosis said:
Ok cool thats really quite obvious.. but it brings me back to my first question - how do you name the properties to anything other than X and Y?
I want to list my point properties as Right and Bottom:
You can't do that, because the Point structure is defined in the Framework and X/Y is the named coordinate members already there.
The other option would be to define your own structure/class with your own members (named as wished) and set up the ExpandableObjectConverter as described in article. This would be awkward when working with code that needed to use your custom point in places where system Point were required (you'd have to provide yet another property that translated to system Point).
 
Just as an added note, if you want to use it to appear as bottom and right you can use an enumeration..
VB.NET:
Public Enum Direction
Bottom
Right
End Enum

Then in your declaring class

VB.NET:
Private M_Direction as Direction
 
Public property Direction
Get 
Return m_Direction
End Get
Set (byval Value as Direction)
M_direction = value
end set
End Property


Then you just need to query the value of M_direction and perform an action accordingly.
 
Oh, it appeared to me that mafrosis wanted a Point property where integer value structure members X and Y was named Left and Bottom instead... :)
 
JohnH, thats exactly what I wanted to do. I had a go on the tutorial but it seems like a bit of overkill writing all that code just to get X and Y displayed as Bottom and Right! Maybe ill get round to it when ive got more time.. ;)

Ended up using a Size class - Width and Height are close enough.

Thanks for all your help
mafro
 
Back
Top