Windows Forms: Metro Progress Bar in C#

By FoxLearn 6/8/2017 8:33:46 PM   12.05K
Metro Progress Bar in C# using Metro Framework, Modern UI

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "MetroProgressBarDemo" and then click OK

metro progress barStep 2: Right click on your project select Manage NuGet Packages -> Search metro framework -> Install

install metro frameworkIf you don't see the metro framework in your toolbox, you can view How to download and install metro framework

Step 3: Design your metro form as below

metro framework c#

Step 4: Create a ProgressReport class to display the complete percentage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MetroProgressBarDemo
{
    public class ProgressReport
    {
        public int PercentComplete { get; set; }
    }
}

Add code to handle your form as below

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace MetroProgressBarDemo
{
    public partial class Form1 : MetroFramework.Forms.MetroForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Create a task to process import data
        Task ProcessImport(List<string> data, IProgress<ProgressReport> progress)
        {
            int index = 1;
            int totalProgress = data.Count;
            var progressReport = new ProgressReport();
            //Create a new thread
            return Task.Run(() =>
            {
                for(int i = 0; i < totalProgress; i++)
                {
                    progressReport.PercentComplete = index++ * 100 / totalProgress;
                    progress.Report(progressReport);
                    Thread.Sleep(15);//Simulate
                    //Insert data to database
                }
            });
        }

        private async void btnStart_Click(object sender, EventArgs e)
        {
            //Init data
            List<string> list = new List<string>();
            for (int i = 0; i < 1000; i++)
                list.Add(i.ToString());
            lblProcess.Text = "Working...";
            var progressReport = new Progress<ProgressReport>();
            progressReport.ProgressChanged += (o, report) =>
            {
                //Update your percentage
                lblProcess.Text = string.Format("Processing...{0}%", report.PercentComplete);
                metroProgressBar.Value = report.PercentComplete;
                metroProgressBar.Update();
            };
            //Process import data
            await ProcessImport(list, progressReport);
            lblProcess.Text = "Done !!!";
        }
    }
}

VIDEO TUTORIALS