0

I'm stuck and I hope someone can help me. I want to change my Enum Items name so they are displayed different in the Combobox.

Public Enum Conditions
    VeryGood
    Good 
    JustOkey
    Poor
End Enum

In my Main:

ComboBox_Cond.Items.AddRange([Enum].GetNames(GetType(Conditions)))
ComboBox_Cond.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox_Cond.SelectedIndex = CInt(Conditions.JustOkey)

In my combobox:

Private Sub ComboBox_Cond_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_Cond.SelectedIndexChanged
    ComboBox_Cond.Items.Add(Conditions.VeryGood).ToString("Very good")
    ComboBox_Cond.Items.Add(Conditions.Good).ToString("Good")
    ComboBox_Cond.Items.Add(Conditions.JustOkey).ToString("Just okey")
    ComboBox_Cond.Items.Add(Conditions.Poor).ToString("Poor")
End Sub

The result of this is just as you can imagine, I got both the "raw" Enum names and the ToString names in my combobox list.

Thank you in advance!

Televinken
  • 35
  • 6
  • `ComboBox_Cond.Items.AddRange([Enum].GetNames(GetType(Conditions)))` should populate with just the names unless you have other code doing something (like the second code block), the selection can be converted back using `Enum.Parse` very simply – Ňɏssa Pøngjǣrdenlarp Oct 23 '14 at 15:11
  • Sorry if was unclear. I just want the combobox to display the ToString names, as "Very good" instead of "VeryGood". – Televinken Oct 23 '14 at 15:16
  • @Televinken: Did you check my answer? Looks like exactly what you need. – Victor Zakharov Oct 23 '14 at 15:23
  • See [How to get C# Enum description from value?](https://stackoverflow.com/questions/2650080/how-to-get-c-sharp-enum-description-from-value) or [C# - How to Display Friendly Names for Enumerations](https://www.codingame.com/playgrounds/2487/c---how-to-display-friendly-names-for-enumerations) to use attributes and extensions to accomplish a similar goal. Those are both obviously for C# though, so you'd have to translate. – gremlin Jul 20 '22 at 17:22

1 Answers1

1

You can have a (key,value) pair, so the easiest would probably be creating a dictionary:

Dim dict As New Dictionary(Of Conditions, String)
With dict
  .Add(Conditions.VeryGood, "Very good")
  '...
  .Add(Conditions.Poor, "Poor")
End With

Then use data binding instead of ComboBox_Cond.Items.AddRange:

ComboBox_Cond.ValueMember = "Key"
ComboBox_Cond.DisplayMember = "Value"
ComboBox_Cond.DataSource = dict.ToList

You can then access the Conditions value using ComboBox_Cond.SelectedValue.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • Thank you @Neolisk. I'm kind of new to this, where do I write the Dictionary, in the top of my Main Class? – Televinken Oct 23 '14 at 15:29
  • @Televinken: You don't need it as a class variable, from what I see in your code. You can put it **instead** of `ComboBox_Cond.Items.AddRange` and see if it works. `ComboBox_Cond.SelectedValue` will get you `Conditions` value, which I think you are after. The point here is that you don't have to convert back, it will be done by .NET for you. – Victor Zakharov Oct 23 '14 at 15:35