C# Collections : Dictionary – Populate MetroComboBox

How to populate a MetroComboBox from Dictionary in C#

Let's see how to populate a MetroComboBox with data from a dictionary.

Users can then select an item from the metrocombobox and show the selected item in a MessageBox. To do this we handle the selectedIndexChangedEvent of the MetroComboBox.

Let's start.

1. Create Empty Project

  1. Open up Visual Studio and proceed over to File --> New Project
  2. A new dialog popus up like this:
  3. Give our empty project a name and click OK. An empty project will get generated for us.

2. Install Metro Framework

If you haven't installed it yet. If you have then just add reference dlls to your project.

  1. Right click the references section of your project and select manage nuget packages.A dialog popups up.
  2. Search "Metro Framework" by Dennis Magno and click install as below.

3. Add Metro Framework DLLs to Project

We now need to add metro framework dlls to our project to use metro framework.

Next right click the references section of your project and add the metroframework dlls from the packages directory onto which they were installed. You may need to search that directory in your computer.

4. Make project a Windows Project

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

5. Program.cs

We have one class, Program.cs.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Controls;
using MetroFramework.Forms;

namespace MetroComboBoxDictionary
{
    static class Program
    {
        private static MetroComboBox metroComboBox;
        public static void Main()
        {
            MetroForm myForm = new MetroForm
            {
                Text = "MetroComboBox Dictionary Example",
                ClientSize = new Size(564, 420),
            };

            metroComboBox = new MetroComboBox { Location = new Point(164, 176) };
            Dictionary<int, String> nebulae = new Dictionary<int, String>
            {
                {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"}

            };
            BindingSource bindingSource=new BindingSource(nebulae.Values,null);
            metroComboBox.DataSource = bindingSource;

            metroComboBox.SelectedIndexChanged += metroComboBox_SelectedIndexChanged;

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

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

Best Regards.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *