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?
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)))
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With