C# Metro - ComboBox - Fill From Enum

| Page Views: 303

In this class we see how to populate a metro farework combobox from enum.

ComboBox Enum

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.

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.

MetroFramework ComboBox

MetroFramework ComboBox apply metro visual styles to combobox. The result is a modern combobox that still work the way we want.

Let's go.

1. Create 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 it a name and click OK. A porject 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 them like these:

MetroFramewok References

These allow us to use metro framework both at code level and with the designer.

  1. Now if you look at the Windows designer toolbox you'll see the metro controls.You can then add them via the designer.

MetroFramewok ToolBox

4. Add Metro Framework Controls/Components

Add it via the designer from the toolbox then go to your Form.cs and make it derive from MetroFramework.Forms.MetroForm like below:

    public partial class Form1 : MetroForm{..}

5. Our classes

Some of these classes will be generated by the designer. The only class we customize is the Form1.cs.

(a). Form1.cs

This is the class we edit and add our logic code.

First add the using statements:

using System;
using MetroFramework;
using MetroFramework.Forms;

Then define our namespace:

namespace ComboBox_Enum

Then our class inside the namespace:

    public partial class Form1 : MetroForm{...}

Then define one instance boolean field:

        private readonly bool everythingLoaded;

Then method to bind our enum names to our combobox:

        private void bindData()
            enum_ComboBox.DataSource = Enum.GetNames(typeof(Propellants));

You can see we retrieve the enum names via the GetNames() method of Enum class.

Go then to your designer and add one event: the selectedIndexChanged event. Then we come and handle that event as below:

        private void enum_ComboBox_SelectedIndexChanged(object sender, EventArgs e)
            if (everythingLoaded)
                MetroMessageBox.Show(this, enum_ComboBox.SelectedItem.ToString(), "Enum ComboBox");

In the above case we are showing a MetroMessageBox when user selects a given item in the combobox.

We now need to define an enum: Just create it inside the above namespace but maybe outside the class:

    enum Propellants
        Laser =30,

(b). Form1.Designer.cs

Partial class generated by Windows Designer. This class has the following roles:

No. Role
1. Is a partial class for Form1.cs class.
2. Will clean up resources occupied by the components as need be via the Dispose() method.
3. Initializes all UI components we use in our project and that we add via the designer.
namespace ComboBox_Enum
    partial class Form1
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
            if (disposing && (components != null))

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
            this.enum_ComboBox = new MetroFramework.Controls.MetroComboBox();
            // enum_ComboBox
            this.enum_ComboBox.FormattingEnabled = true;
            this.enum_ComboBox.ItemHeight = 23;
            this.enum_ComboBox.Location = new System.Drawing.Point(164, 176);
            this.enum_ComboBox.Name = "enum_ComboBox";
            this.enum_ComboBox.Size = new System.Drawing.Size(244, 29);
            this.enum_ComboBox.TabIndex = 0;
            this.enum_ComboBox.UseSelectable = true;
            this.enum_ComboBox.SelectedIndexChanged += new System.EventHandler(this.enum_ComboBox_SelectedIndexChanged);
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(564, 417);
            this.Name = "Form1";
            this.Text = "ComboBox Enum";



        private MetroFramework.Controls.MetroComboBox enum_ComboBox;

(c). Program.cs

This is main class containing our main method. It is a static class.

This class is also generated by Visual Studio.

Here are the roles of this class: No. Role
1. It is our main class. It has main method that will get executed first when we run the project.Therefore it's the entry point of our application.
2. Enables the visual styles of the Application via Application.EnableVisualStyles().
3. Instantiates and runs the Form1.cs class Application.Run(new Form1()).
using System;
using System.Windows.Forms;

namespace ComboBox_Enum
    static class Program
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            Application.Run(new Form1());

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:


What do You Think

Previous Post Next Post