C# Windows Forms - ComboBox - Fill From Hashtable

| Page Views: 429

Let's see how to populate a ComboBox control with a Hashtable structure as our data source.

We alse see how to get the selected item from our combobox and display it in a messagebox.

What is a ComboBox

A combobox is a User Interface control that displays items in a dropdown menu.

ComboBoxes allow users to select one item at a time.

Example Demo

Why ComboBox?

No. Advantage
1. ComboBox display lists of items in a small space.
2. ComboBox allow users to select one item at a time.
3. ComboBox allows us to handle IndexSelectionChanged events.

Let's go.

1. Create Empty Project

  1. Open up Visual Studio and proceed over to File --> New Project

  2. A new dialog popus up like this:

Create Project

  1. Give our empty project a name and click OK. An empty project will get generated for us.

2. Make project a Windows Project

Just right click on your project choose properties and set output type to Windows Application:

MetroFramewok References

3. Progam.cs

We'll have one class Program.cs with several methods including the Main method.

using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace ComboBoxHashtable
    static class Program
        private static ComboBox comboBox;
        public static void Main()
            Form myForm = new Form
                Text = "WinForms ComboBox HashTable Example",
                ClientSize = new Size(564, 420),
                BackColor = Color.LightSalmon

            comboBox = new ComboBox { Location = new Point(164, 176) ,DropDownStyle = ComboBoxStyle.DropDownList};
            Hashtable nebulae=new Hashtable
                { "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"}


            BindingSource bindingSource = new BindingSource(nebulae.Keys, null);
            comboBox.DataSource = bindingSource;

            comboBox.SelectedIndexChanged += comboBox_SelectedIndexChanged;


        static void comboBox_SelectedIndexChanged(object sender, EventArgs e)

Best Regards,Oclemy.

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.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post