C# Metro - ComboBox - Fill From Enum

March 14, 2018 Oclemy C# ComboBox, C# Enum 4 minutes, 30 seconds

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()
        {
            //BIND ENUM NAMES TO COMBOBOX
            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 INSIDE NAMESPACE
    enum Propellants
    {
        Chemical=10,
        Solar=20,
        Laser =30,
        Nuclear=40,
        Plasma=50,
        AntiMatter=60
    }
}

(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))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #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();
            this.SuspendLayout();
            // 
            // 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.Controls.Add(this.enum_ComboBox);
            this.Name = "Form1";
            this.Text = "ComboBox Enum";
            this.ResumeLayout(false);

        }

        #endregion

        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>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Best Regards, Oclemy.

Comments