Skip to content

Create an Enum ComboBox in VB.Net

  • by

There are many things to consider when you write code. Often times you are focused on what you are trying to deliver to your customer. Lately, I’ve been trying to focus more on what will make my job as a programmer easier. Mostly that boils down to two things I like in my code, re-usability and ease of updating. In the past I’ve found myself writing the same code over and over again, and when it came time for changes, updating the same code in a bunch of different places.

Consider this, you have a reasonably large number of forms/controls in your application and many of them contain a drop-down list of the same set of values. Usually, you might just edit each combo box and type in the values for each instance of the list on each form/control it appears on. However, if you need to update those values, you now have to do it in many places and you may forget one and thus introduce a bug into your program. One solution is to create a drop-down list that automatically loads its values from an enumeration. That way every instance of this list will always be the same and can be added by just dropping it onto the form/control without having to set its properties.

To do this, you need to first create a new class that inherits from ComboBox and accepts a type parameter T. This will be your generic base class that will do all the work.

Public Class EnumComboBox(Of T)
    Inherits ComboBox
End Class

Since you are inheriting from ComboBox, your new control has most of the functionality it needs already. Next, it’s just a matter of using reflection over the enumeration type passed in the type parameter T to fill in the DataSource property. You can do this in the constructor, and it looks like this:

    Public Sub New()
        MyBase.New()
        'Drop down list style, no text entry
        Me.DropDownStyle = ComboBoxStyle.DropDownList
        'Get type variable
        Dim EnumType As Type = GetType(T)
        'Check if it really is a enum
        If Not EnumType.IsEnum Then
            Throw New Exception(String.Format("Type {0} is not an enumeration.", EnumType.Name))
        End If
        'Get enum values
        Dim Values() As T = [Enum].GetValues(EnumType)
        'Setup datasource
        'Use linq query 
        Dim NewItems = From x In Values Select Key = [Enum].GetName(EnumType, x), Value = x
        'Set datasource to NewItems
        DataSource = NewItems.ToList
        'set display and value members
        DisplayMember = "Key"
        ValueMember = "Value"
    End Sub

First we are setting the DropDownStyle property to DropDownList, as this disables the free-entry text box portion of the ComboBox. The next statement, Dim EnumType As Type = GetType(T), retrieve a type variable that contains the reflection information we need. We also added a check to the IsEnum property just to make sure that the type parameter is actually and enumeration type. The line, Dim Values() As T = [Enum].GetValues(EnumType), is pretty simple and just retrieves an array of all the values in the enumeration. Next, we are using a LINQ statement to pair up each value with its name into an IEnumerable of a generic type. Note that we gave the properties in the generic type the specific names Key and Value. Then we set the Datasource property to be NewItems.ToList. Setting the Datasource directly to the LINQ query object doesn’t work, as the ComboBox class doesn’t seem to recognize query objects as a valid data source. ToList processes the query and converts it into a list object. Lastly, we just set the DisplayMember and ValueMember properties to “Key” and “Value”.

There are two other additions to this control. One is simply a property that exposes SelectedValue as type T rather that just plain object. This mostly just helps when coding against the control.

    Public ReadOnly Property EnumValue As T
        Get
            Return SelectedValue
        End Get
    End Property

The other addtion is just a hack to work around some problems with the forms designer. Basically what happens is the designer tries to serialize properties from the controls when you place them on a form/control. Well, if you try to serialize our datasource property, it will fail since it can’t serialize an anonymous type. That error will prevent you from adding your control to a form/control.

    <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)>
    Shadows Property DataSource
        Get
            Return MyBase.DataSource
        End Get
        Set(ByVal value)
            MyBase.DataSource = value
        End Set
    End Property

That leaves us with the complete control code, shown here.

Public Class EnumComboBox(Of T)
    Inherits ComboBox

    Public Sub New()
        MyBase.New()
        'Drop down list style, no text entry
        Me.DropDownStyle = ComboBoxStyle.DropDownList
        'Get type variable
        Dim EnumType As Type = GetType(T)
        'Check if it really is a enum
        If Not EnumType.IsEnum Then
            Throw New Exception(String.Format("Type {0} is not an enumeration.", EnumType.Name))
        End If
        'Get enum values
        Dim Values() As T = [Enum].GetValues(EnumType)
        'Setup datasource
        'Use linq query 
        Dim NewItems = From x In Values Select Key = [Enum].GetName(EnumType, x), Value = x
        'Set datasource to NewItems
        DataSource = NewItems.ToList
        'set display and value members
        DisplayMember = "Key"
        ValueMember = "Value"
    End Sub

    <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)>
    Shadows Property DataSource
        Get
            Return MyBase.DataSource
        End Get
        Set(ByVal value)
            MyBase.DataSource = value
        End Set
    End Property

    Public ReadOnly Property EnumValue As T
        Get
            Return SelectedValue
        End Get
    End Property
End Class

Now, another issue with the Visual Studio form designer, is that you cannot add a generically defined control through the control toolbox. To get it to work in the designer you just have to create a non-generic class, like this.

Public Class ComboBoxExample
    Inherits EnumComboBox(Of ExampleEnum)

End Class

At this point, if you build your project, ComboBoxExample should appear in your toolbox to be dropped onto your control.

That’s it. You now have a reusable drop down list, based on an enumeration that, no matter where it’s used will always reflect the correct values.

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available