Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load values of enum type into a combobox

Given the following enum:

Enum enumExample
  world
  oblivion
  holiday
End Enum

I can add its values to a list of ComboBox items like this:

combo.Items.Add(enumExample.holiday)
combo.Items.Add(enumExample.oblivion)
combo.Items.Add(enumExample.world)

Is there a shorter way?

like image 630
whytheq Avatar asked Jan 19 '13 21:01

whytheq


2 Answers

You can use Enum.GetValues to get a list of values for an enum then iterate the result:

For Each i In  [Enum].GetValues(GetType(EnumExample))
  combo.Items.Add(i)
Next

Or, as mentioned by @Styxxy:

combo.Items.AddRange([Enum].GetValues(GetType(EnumExample)))
like image 59
NinjaNye Avatar answered Oct 14 '22 11:10

NinjaNye


Why not just use:

Enum enumExample
  world
  oblivion
  holiday
End Enum

ComboBox1.DataSource = [Enum].GetValues(GetType(enumExample))

This is what I used and it seems to have worked.

like image 15
Culpepper Avatar answered Oct 14 '22 10:10

Culpepper