C# Windows Forms - ComboBox - Fill From Hashtable

February 4, 2018 Oclemy C# ComboBox, C# Hashtable 1 minute, 38 seconds

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;

            myForm.Controls.Add(comboBox);
            Application.Run(myForm);
        }

        static void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(comboBox.SelectedItem.ToString());
        }
    }
}

Best Regards,Oclemy.

Comments