VB.NET ComboBox - Fill From Dictionary


In this tutorial we see how to populate a VB.NET combobox from a dictionary of data.

Then we handle teh combobox' selected index changed event and show the item in a messagebox.

Let's go.

Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms

Module Module1
    Private WithEvents comboBox As ComboBox

    Sub Main()
        Dim myForm As Form = New Form With {.Text = "WinForms ComboBox Dictionary Example", .ClientSize = New Size(564, 420), .BackColor = Color.SeaGreen}
        comboBox = New ComboBox With {.Location = New Point(164, 176), .DropDownStyle = ComboBoxStyle.DropDownList}
        Dim nebulae As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String) From {{1, "Horse Head"}, {2, "Black Widow"}, {3, "Ghost Head"}, {4, "Witch Head"}, {5, "Cat's Eye"}, {6, "Elephant's Trunk"}, {7, "Helix"}, {8, "Rosette"}, {9, "Ant"}, {10, "Orion"}, {11, "Eagle"}, {12, "Own"}, {13, "Ring"}, {14, "Pelican"}, {15, "Cone"}, {16, "Flame"}, {17, "Bumerang"}}
        Dim bindingSource As BindingSource = New BindingSource(nebulae.Values, Nothing)
        comboBox.DataSource = bindingSource
        myForm.Controls.Add(comboBox)
        Application.Run(myForm)
    End Sub

    Private Sub comboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox.SelectedIndexChanged
        MessageBox.Show(comboBox.SelectedItem.ToString())
    End Sub
End Module

Result VB.NET ComboBox Dictionary

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post