SoFunction
Updated on 2025-03-06

C# implements a progress bar function example with percentage

This article describes the C# implementation of the progress bar function with percentage. Share it for your reference, as follows:

Functional requirements:

If a time-consuming calculation process is performed in the program, I want to pop up a progress bar window after the user clicks the button, showing the progress being executed (preferably with a percentage). After the execution is completed, the progress bar window closes and returns to the main program window. The parent form cannot click the action until the child window is closed.

Implementation method:

First design the Form2 progress bar form, place the ProgressBar control progressBar1 and the Label control label1 in the center of Form2. Code:

public partial class Form2 : Form
{
 public Form2(int _Minimum,int _Maximum)//with parameters, indicating the minimum and maximum value of the range of the progress bar {
   InitializeComponent();
   =_Maximum;//Set the maximum range    =  = _Minimum;//Set the minimum range value }
 public void setPos(int value)//Set the current progress value of the progress bar {
   if (value < )//If the value is valid   {
      = value;//Set progress value      = (value * 100 / ).ToString() + "%";//Display percentage   }
   ();//The key point must be added, otherwise the father and son form will be faked to death }
 private void Form2_Load(object sender, EventArgs e)
 {
    = false;//Set the parent form is not available }
 private void Form2_FormClosed(object sender, FormClosedEventArgs e)
 {
    = true;//Reply to the parent form is available }
}

Call Form For1m design, add Button control button1, event code:

private void button1_Click(object sender, EventArgs e)
{
   Form2 fm = new Form2(0,100);
   (this);//Set the parent form   for (int i = 0; i < 100; i++)
   {
     (i);//Set the progress bar position     (100);//Sleep time is 100   }
   ();//Close the form}

Replenish:A friend said that (this): is not supported in vs2003, so you can add an extra parameter to the From2 constructor:

public Form OwnerForm;
public Form2(int _Minimum,int _Maximum,Form _OwnerForm)//with parameters, indicating the minimum and maximum value of the range of the progress bar{
   InitializeComponent();
   =_Maximum;//Set the maximum range    =  = _Minimum;//Set the minimum range value   =_OwnerForm;
}
private void Form2_Load(object sender, EventArgs e)
{
    = false;//Set the parent form is not available}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
    = true;//Reply to the parent form is available}

The corresponding modification in Form1 is:

private void button1_Click(object sender, EventArgs e)
{
   Form2 fm = new Form2(0,100,this);
   ();//Set the parent form   for (int i = 0; i < 100; i++)
   {
     (i);//Set the progress bar position     (100);//Sleep time is 100   }
   ();//Close the form}

For more information about C# related content, please check out the topic of this site:Tutorial on the usage of common C# controls》、《Summary of C# form operation skills》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.