VB.NET ComboBox - Fill From Hashtable


In this tutorial we see how to populate a Visual Basic combobox with data from a dictionary.

Let's go.

Imports System
Imports System.Collections
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 HashTable Example", .ClientSize = New Size(564, 420), .BackColor = Color.LightSalmon}
        comboBox = New ComboBox With {.Location = New Point(164, 176), .DropDownStyle = ComboBoxStyle.DropDownList}
        Dim nebulae As Hashtable = New Hashtable From {{"Horse Head", "HII REGION"}, {"Black Widow", "REFLECTION NEBULAR"}, {"Ghost Head", "HII REGION"}, {"Witch Head", "PLANETARY NEBULAR"}, {"Cat's Eye", "DARK NEBULAR"}, {"Elephant's Trunk", "HII REGION"}, {"Helix", "SUPERNOVA REMNANTS"}, {"Rosette", "REFLECTION NEBULAR"}, {"Ant", "PLANETARY NEBULAR"}, {"Orion", "HII REGION"}, {"Eagle", "REFLECTION NEBULAR"}, {"Roesette", "PLANETARY NEBULAR"}, {"Bernad 68", "SUPERNOVA REMNANTS"}, {"Butterfly", "DARK NEBULAR"}, {"Snake", "DARK NEBULAR"}, {"Own", "REFLECTION NEBULAR"}, {"Ring", "SUPERNOVA REMNANTS"}, {"Pelican", "PLANETARY NEBULAR"}, {"Cone", "DARK NEBULAR"}, {"Flame", "REFLECTION NEBULAR"}, {"Bumerang", "SUPERNOVA REMNANTS"}}
        Dim bindingSource As BindingSource = New BindingSource(nebulae.Keys, 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 Hashtable

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