Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can I order the enum values in propertygrid by descripion?

VB
Public Class myObject
    Public Enum eFolderOrder

        <ComponentModel.Description("c")> l= 0
        <ComponentModel.Description("a")> m= 1
        <ComponentModel.Description("b")> n= 2
    End Enum


    <TypeConverter(GetType(DescriptionConverter))> <System.ComponentModel.DisplayName("MyProp")> Public Property test As eFolderOrder = eFolderOrder.Description
End Class


VB
Class DescriptionConverter
    Inherits EnumConverter
    Private _enumType As Type
    ''' <summary />Initializing instance</summary />
    ''' <param name=""type"" />type Enum</param />
    ''' this is only one function, that you must
    ''' change. All another functions for enums
    ''' you can use by Ctrl+C/Ctrl+V
    Public Sub New(type As Type)
        MyBase.New(type)
        _enumType = type
    End Sub

    Public Overrides Function CanConvertTo(context As ITypeDescriptorContext, destType As Type) As Boolean
        Return destType = GetType(String)
    End Function

    Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destType As Type) As Object
        Dim fi As FieldInfo = _enumType.GetField([Enum].GetName(_enumType, value))
        Dim dna As DescriptionAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)

        If dna IsNot Nothing Then
            Return dna.Description
        Else
            Return value.ToString()
        End If
    End Function

    Public Overrides Function CanConvertFrom(context As ITypeDescriptorContext, srcType As Type) As Boolean
        Return srcType = GetType(String)
    End Function

    Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object
        For Each fi As FieldInfo In _enumType.GetFields()
            Dim dna As DescriptionAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)

            If (dna IsNot Nothing) AndAlso (DirectCast(value, String) = dna.Description) Then
                Return [Enum].Parse(_enumType, fi.Name)
            End If
        Next
        Return [Enum].Parse(_enumType, DirectCast(value, String))
    End Function
End Class


The shown order is:

c
a
b

but it supposed to be:

a
b
c


Thanks in advance,
Daniel
Posted

Override the Comparer property[^] with a custom IComparer instance that sorts as you want it to.

Something like this should work:
VB.NET
Class EnumDescriptionComparer : Implements IComparer
    Private ReadOnly _enumType As Type
    
    Public Sub New(ByVal enumType As Type)
        _enumType = enumType
    End Sub
    
    Private Function GetDescription(ByVal value As Object) As String
        Dim fi As FieldInfo = _enumType.GetField([Enum].GetName(_enumType, value))
        Dim dna As DescriptionAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(DescriptionAttribute)), DescriptionAttribute)
        Return If(dna Is Nothing, value.ToString(), dna.Description)
    End Function
    
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Dim xDescription As String = GetDescription(x)
        Dim yDescription As String = GetDescription(y)
        Return StringComparer.OrdinalIgnoreCase.Compare(xDescription, yDescription)
    End Function
End Class

Class DescriptionConverter : Inherits EnumConverter
    Private ReadOnly _enumType As Type
    Private ReadOnly _comparer As IComparer
    
    Public Sub New(ByVal type As Type)
        MyBase.New(type)
        _enumType = type
        _comparer = new EnumDescriptionComparer(type)
    End Sub
    
    Protected Override ReadOnly Property Comparer As IComparer
        Get
            Return _comparer
        End Get
    End Property
    
    ...
End Class
 
Share this answer
 
Comments
Maciej Los 3-Nov-15 13:53pm    
Looks perfect, 5!
schlumpfger 4-Nov-15 5:37am    
Thank you! Works perfect!
In addition to solution 1 by Richard Deeming[^], i'd recommend to read this: Adding Descriptions to your Enumerations[^]. Even if this article uses c# code, you're able to convert it to vb.net.
 
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