Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

Extending the PropertyGrid with a new PropertyTab

Rate me:
Please Sign up or sign in to vote.
4.89/5 (39 votes)
15 Jan 2007CPOL8 min read 121.3K   4.1K   128  
Add a PropertyTab showing the fields of an object and overlay icons to the PropertyGrid
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Globalization
Imports System.Reflection

Namespace PropertyGridEx

    Public Class UIListboxEditor
        Inherits UITypeEditor

        Private bIsDropDownResizable As Boolean = False
        Private WithEvents oList As New ListBox
        Private oSelectedValue As Object = Nothing
        Private oEditorService As IWindowsFormsEditorService

        Public Overloads Overrides Function GetEditStyle(ByVal context As _
        ITypeDescriptorContext) As UITypeEditorEditStyle
            If Not context Is Nothing AndAlso Not context.Instance Is Nothing Then
                Dim attribute As UIListboxIsDropDownResizable = context.PropertyDescriptor.Attributes(GetType(UIListboxIsDropDownResizable))
                If Not attribute Is Nothing Then
                    bIsDropDownResizable = True
                End If
                Return UITypeEditorEditStyle.DropDown
            End If
            Return UITypeEditorEditStyle.None
        End Function

        Public Overloads ReadOnly Property IsDropDownResizable() As Boolean
            Get
                Return bIsDropDownResizable
            End Get
        End Property

        <RefreshProperties(RefreshProperties.All)> _
        Public Overloads Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
            If context Is Nothing OrElse provider Is Nothing _
            OrElse context.Instance Is Nothing Then
                Return MyBase.EditValue(provider, value)
            End If

            oEditorService = provider.GetService(GetType(IWindowsFormsEditorService))
            If Not oEditorService Is Nothing Then

                ' Get the Back reference to the Custom Property
                Dim oDescriptor As CustomProperty.CustomPropertyDescriptor = context.PropertyDescriptor
                Dim cp As CustomProperty = oDescriptor.CustomProperty

                ' Declare attributes
                Dim datasource As UIListboxDatasource
                Dim valuemember As UIListboxValueMember
                Dim displaymember As UIListboxDisplayMember

                ' Get attributes
                With context.PropertyDescriptor
                    datasource = .Attributes(GetType(UIListboxDatasource))
                    valuemember = .Attributes(GetType(UIListboxValueMember))
                    displaymember = .Attributes(GetType(UIListboxDisplayMember))
                End With

                With oList
                    .BorderStyle = BorderStyle.None
                    .IntegralHeight = True

                    If Not datasource Is Nothing Then
                        .DataSource = datasource.Value
                    End If

                    If Not displaymember Is Nothing Then
                        .DisplayMember = displaymember.Value
                    End If

                    If Not valuemember Is Nothing Then
                        .ValueMember = valuemember.Value
                    End If

                    If Not value Is Nothing Then
                        If value.GetType.Name = "String" Then
                            oList.Text = value
                        Else
                            oList.SelectedItem = value
                        End If
                    End If

                End With

                AddHandler oList.SelectedIndexChanged, AddressOf Me.SelectedItem

                oEditorService.DropDownControl(oList)
                If oList.SelectedIndices.Count = 1 Then
                    cp.SelectedItem = oList.SelectedItem
                    cp.SelectedValue = oSelectedValue
                    value = oList.Text
                End If
                oEditorService.CloseDropDown()
            Else
                Return MyBase.EditValue(provider, value)
            End If

            Return value

        End Function

        Private Sub SelectedItem(ByVal sender As Object, ByVal e As EventArgs)
            If Not oEditorService Is Nothing Then
                If Not oList.SelectedValue Is Nothing Then oSelectedValue = oList.SelectedValue
                oEditorService.CloseDropDown()
            End If
        End Sub

        Public Class UIListboxDatasource
            Inherits Attribute
            Private oDataSource As Object
            Public Sub New(ByRef Datasource As Object)
                oDataSource = Datasource
            End Sub
            Public ReadOnly Property Value() As Object
                Get
                    Return oDataSource
                End Get
            End Property
        End Class

        Public Class UIListboxValueMember
            Inherits Attribute
            Private sValueMember As String
            Public Sub New(ByVal ValueMember As String)
                sValueMember = ValueMember
            End Sub
            Public Property Value() As String
                Get
                    Return sValueMember
                End Get
                Set(ByVal value As String)
                    sValueMember = value
                End Set
            End Property
        End Class

        Public Class UIListboxDisplayMember
            Inherits Attribute
            Private sDisplayMember As String
            Public Sub New(ByVal DisplayMember As String)
                sDisplayMember = DisplayMember
            End Sub
            Public Property Value() As String
                Get
                    Return sDisplayMember
                End Get
                Set(ByVal value As String)
                    sDisplayMember = value
                End Set
            End Property

        End Class

        Public Class UIListboxIsDropDownResizable
            Inherits Attribute
        End Class

    End Class

End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior)
Germany Germany
Carsten started programming Basic and Assembler back in the 80’s when he got his first C64. After switching to a x86 based system he started programming in Pascal and C. He started Windows programming with the arrival of Windows 3.0. After working for various internet companies developing a linguistic text analysis and classification software for 25hours communications he is now working as a contractor.

Carsten lives in Hamburg, Germany with his wife and five children.

Comments and Discussions