.Net leave Combobox option selected by default


I have a combobox that takes the DataSource of an Enum, the order of the enum I do not like, but I can not modify said enum, I would like to have an option (the third one) preset by default in the combobox. How could I do it?

I ended up using SelectedIndex to put the one that interests me by default, but how could I rearrange it?

asked by Omar 18.07.2018 в 23:23

2 answers


Hi, you have to add the System.Linq lib to be able to query the ENUM

The first thing in the instruction gets the Enum values

(CType([Enum].GetValues(GetType(EnumParaCombo)), EnumParaCombo())).Select(Function(c) c.ToString)

and then we arrange the order by means of the instruction:

 OrderBy(Function(x) x).ToList

I leave you the test code that has a messy enum and at the end it prints it as an ordered list.

Imports System.Linq

Module Module1
    Sub Main()
        Dim enums As List(Of String) = (CType([Enum].GetValues(GetType(EnumParaCombo)), 
                                        EnumParaCombo())).Select(Function(c) c.ToString).
                                        OrderBy(Function(x) x).ToList

        For Each senum In enums
    End Sub
End Module

Public Enum EnumParaCombo
    OptionC = 0
    OptionA = 1
    OptionM = 2
    OptionF = 3
End Enum

The result of running this program is this

answered by 20.07.2018 / 23:59

You do not understand your question much, but try like this:

 Combobox.SelectedIndex = 0;
answered by 19.07.2018 в 18:53