C# MetroFramework - MetroComboBox - Fill From ArrayList


In this tutorial we see how to populate a metrocombobox with data from an arraylist.

ComboBoxes normally allow us display data in a dropdown view where the user can select a single item.

We will also listen to the IndexSelectionChange event of our metrocombobox.

Example Demo

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. 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.

Install MetroFramewok

3. Add Metro Framework DLLs to Project

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

  1. 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.

Add MetroFramewok DLLs

  1. You should have it like these:

MetroFramewok References

4. Make project a Windows Project

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

MetroFramewok References

5. Program.cs

We have one class, Program.cs.

This is a static class and contains a static main method that will be launched when we launch our application.

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

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

            metroComboBox = new MetroComboBox { Location = new Point(164, 176) };
            ArrayList nebulae = new ArrayList { "Horse Head", "Black Widow","Ghost Head","Cat's Eye","Helix","Rosette", "Ant", "Orion","Butterfly", "Eagle","Own", "Ring","Pelican", "Witch Head","Bumerang" };
            metroComboBox.DataSource = nebulae;

            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. 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. 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