Thursday, February 12, 2015

vb.net Enums to combobox and back

Having Enums is nice, having the in a combobox without writing a lot of manual code is even nicer.
Here is how its done:

On a Form add a combobox and a textbox (multiline).

Add code below:
Enum ePets
        eA_Dog = 0
        eA_Cat = 1
        eA_Hamster = 7
        eA_GoldFish = 33

End Enum
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       

        ' Make it a list so user cannot changes text etc
         ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        

        ' Load Enums to Combobox
        ComboBox1.Items.AddRange([Enum].GetNames(GetType(ePets)))
        ComboBox1.SelectedIndex = 0 ' Point to first item
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ' Get text from selected combo, parse it to a Enum instead
        Dim a As ePets = [Enum].Parse(GetType(ePets), ComboBox1.Text)
        Dim y As Integer = a
        Dim txt As String = "ComboText: " + ComboBox1.Text + vbCrLf
        txt += "ComboIndex: " + ComboBox1.SelectedIndex.ToString + vbCrLf
        txt += "Enum + Value: " + a.ToString + ", " + Integer.Parse(a).ToString
        TextBox1.Text = txt

        ' Convert y integer to Enum
        Dim a1 As ePets = [Enum].Parse(GetType(ePets), y)
        Debug.Print(a1.ToString)
    End Sub


Enjoy.

No comments:

MySQL to MariaDB

 Here is a good description how to migrate to MariaDb MySQL to MariaDb Example of how to dump data from MySQL port=3307: mysqldump --user=ro...