C# MetroFramework - MetroListView - Fill From HashSet

March 18, 2018 Oclemy C# ListView, C# HashSet 2 minutes, 3 seconds

In this example we see how to populate a MetroListView from a HashSet as our collection.

We then see how to handle the MetroListView's ItemSelectionChanged events therefore showing a Message Box.

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.

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

namespace MetroListViewHashSet
{
    static class Program
    {
        private static MetroListView metroListView;
        public static void Main()
        {
            setupListView();
            populateData();
            createMetroForm();
        }

        private static void createMetroForm()
        {
            MetroForm myForm = new MetroForm
            {
                Text = "MetroListView HashSet - Nebulae",
                ClientSize = new Size(564, 520)
            };

            myForm.Controls.Add(metroListView);
            Application.Run(myForm);
        }
        private static void setupListView()
        {
            metroListView = new MetroListView { Location = new Point(30, 70), Size = new Size(400, 400), View = View.Details, FullRowSelect = true, Alignment = ListViewAlignment.SnapToGrid };
            metroListView.Columns.Add("NEBULAR", 200);
            metroListView.Columns.Add("CATEGORY", 200);
            metroListView.MultiSelect = false;
            metroListView.ItemSelectionChanged += metroListView_ItemSelectionChanged;
        }

        static void metroListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                ListViewItem listViewItem = e.Item;
                MessageBox.Show(listViewItem.SubItems[0].Text);
            }
        }

        private static void populateData()
        {
            HashSet<string> nebulae = new HashSet<string>
            {
                "Horse Head", "Black Widow", "Ghost Head", "Cat's Eye", "Elephant's Trunk", "Helix", "Rosette","Snake","Bernad 68",
                "Ant", "Orion", "Butterfly", "Eagle", "Own", "Ring", "Pelican", "Cone", "Flame", "Witch Head", "Bumerang"
            };
            HashSet<string> nebularCategory = new HashSet<string>
            {
                "HII REGION ", "REFLECTION NEBULAR", "PLANETARY NEBULAR", "SUPERNOVA REMNANTS",
                "DARK NEBULAR"
            };
            Random r = new Random();
            foreach (string nebular in nebulae)
            {
                metroListView.Items.Add(new ListViewItem(new[] { nebular, nebularCategory.ToArray()[r.Next(0, 5)], (r.Next(9, 9999)).ToString() }));
            }
        }
    }
}

Best Regards, Oclemy.

Comments