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 1: Click 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
Step 2: Right click on your project select Manage NuGet Packages -> Search metro framework -> Install
If 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
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
- How to create a Metro Live Tiles in C#
- How to Create a Metro ListView in C#
- How to Create a Metro GridView in C#
- How to Create a Modern UI Login Form in C#
- How to Create a Metro TextBox in C#
- How to create a Metro Message Box in C#
- How to use Modern UI Metro Framework in C#
- How to Download and Install Metro Framework
Categories
Popular Posts
How to sign a powershell script
10/03/2024
How to get Credentials in PowerShell?
10/03/2024
How to implement Jint in C#
09/14/2024