Search code examples
vb.netpropertygrid

How to hide expand button in a propertygrid in Windows Forms


I have written my on editor for a property in a propertyGrid. Everything works as expected, but for best user experience i want to hide the expand button on the propertygrid for this property. Can someone tell me how to do that?

How to hide the green marked expand button?

I am using a custom typeconverter

Public Class PropertyLanguageSetupEditor
    Inherits UITypeEditor

    Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle
        Return UITypeEditorEditStyle.Modal
    End Function

    Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
        Try
            Dim svc As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)

            Dim settings As LanguageSettings = value
            Dim oldSettings As LanguageSettings = CType(value, LanguageSettings).Clone

            If Not svc Is Nothing And Not settings Is Nothing Then
                Dim form As New PropertyLanguageSetupWindow(settings)
                If svc.ShowDialog(form) = DialogResult.OK Then
                    value = form.Data
                Else
                    value = oldSettings
                End If
            End If
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
        Return MyBase.EditValue(context, provider, value)
    End Function
End Class

And this is de property which uses the typeConverter

   <DisplayName("Properties Translation Settings"), Editor(GetType(PropertyLanguageSetupEditor), GetType(System.Drawing.Design.UITypeEditor)), TypeConverter(GetType(BrowsableTypeConverter)),
        Description("Settings for the output text"), BrowsableTypeConverter.BrowsableLabelStyleAttribute(BrowsableTypeConverter.LabelStyle.lsEllipsis),
        Category("10 - DXF Export"), Browsable(True)>
    Public Property TranslationSettings As LanguageSettings = New LanguageSettings

My BrowsAbleTypeConverter

Public Class BrowsableTypeConverter
    Inherits ExpandableObjectConverter

    Public Enum LabelStyle
        lsNormal
        lsTypeName
        lsEllipsis
        lsText
    End Enum


    Public Class BrowsableLabelStyleAttribute
        Inherits System.Attribute
        Private eLabelStyle As LabelStyle = LabelStyle.lsEllipsis
        Public Sub New(ByVal LabelStyle As LabelStyle)
            eLabelStyle = LabelStyle
        End Sub
        Public Property LabelStyle() As LabelStyle
            Get
                Return eLabelStyle
            End Get
            Set(ByVal value As LabelStyle)
                eLabelStyle = value
            End Set
        End Property
    End Class

    Public Class BrowsableLabelTextAttribute
        Inherits System.Attribute
        Private strText As String = ""
        Public Sub New(ByVal value As String)
            strText = value
        End Sub
        Public Property Text() As String
            Get
                Return strText
            End Get
            Set(ByVal value As String)
                strText = value
            End Set
        End Property
    End Class

    Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
        Return True
    End Function

    Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
        Dim Style As BrowsableLabelStyleAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelStyleAttribute))
        If Not Style Is Nothing Then
            Select Case Style.LabelStyle
                Case LabelStyle.lsNormal
                    Return MyBase.ConvertTo(context, culture, value, destinationType)
                Case LabelStyle.lsTypeName
                    Return "(" & value.GetType.Name & ")"
                Case LabelStyle.lsEllipsis
                    Return "(...)"
                Case LabelStyle.lsText
                    Dim text As BrowsableLabelTextAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelTextAttribute))
                    If text IsNot Nothing Then
                        Return text.Text
                    End If
            End Select
        End If
        Return MyBase.ConvertTo(context, culture, value, destinationType)
    End Function


End Class

Solution

  • Your TypeConverter derives from ExpandableObjectConverter, which means subproperties will be visible because of its GetPropertiesSupported method returning true.

    In your TypeConverter, you simply need to override GetPropertiesSupported and return false.