Nested Properties

mooman_fl

Centurion
Joined
Nov 3, 2002
Messages
193
Location
Florida, USA
Ok... here is a new one concerning properties on a usercontrol. I would like to have the nifty nested properties on my control (i.e. Size, Location, DockPadding, etc.)

Anyone have a clue as to how this is done?
 
Just to make it clear to dumbos (as I am) ;)

What do you mean with "nested"?

Like "grouped" in the Properties view at designtime?

Or "Hierarchical" at runtime as in

myObject.Layout.Location = New System.Drawing.Point(42,42)

?
 
I think you have to specify a TypeConverter attribute pointing to ExpandableObjectConverter on the class which your property has as its value.
 
I have read a few things that suggested this. However looking at the MSDN help, I am embarrassed to admit, helped very little. It was way over my head. Not to mention that everything I have seen on this is in VC++ (or maybe it was C#). Does anyone know of any information out there that takes this subject from the ground up geared towards a VB user?
 
Here's a quick example I whipped up which does the trick:

Visual Basic:
Imports System.ComponentModel

Public Class ClassWithProperty
    Private _Expandy As ExpandyClass

    Public Sub New()
        _Expandy = New ExpandyClass()
    End Sub

    Public Property Expandy() As ExpandyClass
        Get
            Return _Expandy
        End Get
        Set(ByVal Value As ExpandyClass)
            _Expandy = Value
        End Set
    End Property
End Class

<TypeConverter(GetType(ExpandableObjectConverter))> _
Public Class ExpandyClass
    Private _Variable1, _Variable2 As Integer

    Public Property Variable1() As Integer
        Get
            Return _Variable1
        End Get
        Set(ByVal Value As Integer)
            _Variable1 = Value
        End Set
    End Property

    Public Property Variable2() As Integer
        Get
            Return _Variable2
        End Get
        Set(ByVal Value As Integer)
            _Variable2 = Value
        End Set
    End Property

End Class
 
LOL... your killing me man ;) You just whipped this up... *choke**gasp**goggle*:eek: Have you ever noticed how some people make seem so easy what makes the rest of us lose hair and sleep over?

Thanks!!! I will examine this thoroughly. Once again you have saved the day. :)
 
Hmmm... tried implementing your method of doing this in my project. I got the expandable class made (Padding), I declared it in the declarations section, then in new tired to instantiate it.

I got an error that says: Value of type 'ToolScroll.ToolPanel.Padding' cannot be converted to '1-dimensional array of ToolScroll.ToolPanel.Padding'.

Any idea what this means and why I am getting it? I really haven't done anything different that I can see from your example.
 
That seems to have indeed been the problem.

I find it interesting that it is wrong to do it in your Private declaration, yet it is necessary and automatically added for you when you instantiate.
 
A declaration with () means that you are not declaring a single variable but an array.

Instantiating with () means you are calling the parameterless constructor.

These are two entirely different operations.
 
I was passing by and figured I'd take a look at this interesting topic to see how it was done... I understand the example, and find it interesting actually, heh. Except, well, I got a n00b question..

Why is <TypeConverter(GetType(ExpandableObjectConverter))> needed?
 
The TypeConverterAttribute (attributes being the < > tags that precede
some classes) tells VB what TypeConverter to use
in the designer. In this case, it converts it to a format that the property
window knows how to interpret and respond to accordingly. You
could write your own TypeConverter as well, if you wanted.
 
TypeConverters are classes applied to other classes that enable conversion to and from that class type. A Size structure, for example, has a typeconverter that allows you to enter "100, 200" as a string in the propertygrid and it'll convert it to a size with X being 100 and Y being 200.

The ExpandableObjectConverter class is extremely simple and does little more than tell the propertygrid that this object has properties that should be delved in to.
 
This threat is very old, but, i have one little question about this. Is it possible to give the .ExpandyClass a name in the propertygrid? Now it displays Applicationname.ExpandyClass. I can't find this property?
Giving Categories a name is not a problem, just the expanding item...
 
Back
Top