Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am designing a windows forms control and I need to create nested properties in my control
for example:
I want to have font-size, font-color, and font-family nested properties under Font

If you want to do this, you have to create a class which has the needed Properties. This class should be the Type of your "new Property".

Example :
Imports System.ComponentModel
Imports System.ComponentModel.Design

Public Class PaddingDefinition
    <Description("distance of text to the left side")>
    Property Left As Integer
            Return _left
        End Get
        Set(ByVal value As Integer)
            If _left <> value Then
                _left = value
                RaiseEvent Changed()
            End If
        End Set
    End Property
    Private _left As Integer = 5

    <Description("distance of text to the right side")>
    Property Right As Integer
            Return _right
        End Get
        Set(ByVal value As Integer)
            If _right <> value Then
                _right = value
                RaiseEvent Changed()
            End If
        End Set
    End Property
    Private _right As Integer = 5

    Public Event Changed()

    Sub New()
    End Sub

    Sub New(ByVal left As Integer, ByVal right As Integer)
        _left = left
        _right = right
        RaiseEvent Changed()
    End Sub

    Overrides Function toString() As String
        Return Left.ToString.Trim & ";" & Right.ToString.Trim
    End Function

    Function toPadding() As Padding
        Return New Padding(_left, 0, _right, 0)
    End Function

    Sub fromPadding(setPadding As Padding)
        _left = setPadding.Left
        _right = setPadding.Right
        RaiseEvent Changed()
    End Sub


    Public Class Converter
        Inherits ExpandableObjectConverter

        Public Overloads Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
            If sourceType Is GetType(String) Then
                Return True
                Exit Function
            End If
            'Konvertierung von string zulassen
            Return MyBase.CanConvertFrom(context, sourceType)
        End Function

        Public Overloads Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
            If value.GetType Is GetType(String) Then
                'Konvertierung von string
                Dim s As String = CType(value, String)
                Dim sa As String() = Split(s, ";")
                If sa.Length >= 2 Then
                    Dim p1, p2 As Integer
                    If Integer.TryParse(sa(0), p1) AndAlso Integer.TryParse(sa(1), p2) Then
                        Return New PaddingDefinition(p1, p2)
                        Exit Function
                    End If
                End If
                Throw New FormatException()
            End If

            Return MyBase.ConvertFrom(context, culture, value)
        End Function

    End Class

End Class

and now the use of it :

<Category("Design"), Description("defines the distances inside the control")>
Shadows Property Padding As PaddingDefinition
        Return my_Padding
    End Get
    Set(ByVal value As PaddingDefinition)
        my_Padding = value
    End Set
End Property

Private WithEvents my_Padding As New PaddingDefinition

Private Sub Padding_Changed() Handles my_Padding.Changed
   ' whaz you want to do with it ...
End Sub

I hope this gives you an idea ...

Edit :
I have seen (perhaps a little to late) that the question points to C#.
My Solution is for VB. 8)
But nevertheless - it shows how it could be done ...
Share this answer
Eng.Yahya92 2-Jul-15 1:27am    
I have tried this approach which is writing a class containing the child properties then adding the father class as a propery in my control but it the father property is showing in the property grid alone with uneditable value which is "WindowsFormsApplication.CustomControlProperties"
Eng.Yahya92 2-Jul-15 1:58am    
thank you
problem is solved
I just needed to use this attribute
it drove me crazy untill I thought that what I am doing is wrong
Ralf Meier 2-Jul-15 3:42am    
Sorry - in my example I used a customized converter which derives from the ExpandableObjectConverter ...
Everytime you want to use a class as a Property you have to use this converter (perhaps with the class-declaration as Attribut of the class or with the property).

But I'm glad that I could help you with my VB-Code ... ;)
Eng.Yahya92 2-Jul-15 3:52am    
never mind
thank you for the information
now I am in the middle of a new problem :D
changing the child properties is not reflecting on the control
Ralf Meier 2-Jul-15 5:35am    
No - of course not ...
Therefore I used the Events.
Only the Property inside the class detects (by the Setter) a Change.
There is no such concept of "nested property". It just makes no sense.

You have a kind of nesting if your property is class or struct, which can itself has properties of some other class or struct, and so on.

You did not explain what would nesting "in Control" mean. You need to describe the UI behavior to explain that. I don't know if you really need PropertyGrid, but if you do, customizing it is relatively difficult. The idea is: you should represent your object or the object tree in different form: some intermediate of different type representing your object; this representation object ("surrogate" type) should implement the interface System.ComponentModel.ICustomTypeDescriptor:[^].

Share this answer
You can use the same property grid control that visual studio uses but in your own windows forms - see this article[^] for an example.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900