Example of C Implementation of Progress Bar with Percentage

  • 2021-12-13 09:06:30
  • OfStack

This article illustrates how C # implements the progress bar function with percentages. Share it for your reference, as follows:

Functional requirements:

If a time-consuming calculation process will be executed 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 be clicked until the child window is closed.

Implementation method:

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


public partial class Form2 : Form
{
 public Form2(int _Minimum,int _Maximum)// Minimum and maximum values with parameters representing the range of the progress bar 
 {
   InitializeComponent();
   progressBar1.Maximum=_Maximum;// Set the maximum value of the range 
   progressBar1.Value = progressBar1.Minimum = _Minimum;// Set the minimum value of the range 
 }
 public void setPos(int value)// Set the current progress value of the progress bar 
 {
   if (value < progressBar1.Maximum)// If the value is valid 
   {
     progressBar1.Value = value;// Setting progress values 
     label1.Text = (value * 100 / progressBar1.Maximum).ToString() + "%";// Display percentage 
   }
   Application.DoEvents();// The key point must be added, otherwise the father and son forms will be feigned dead 
 }
 private void Form2_Load(object sender, EventArgs e)
 {
   this.Owner.Enabled = false;// Sets the parent form unavailable 
 }
 private void Form2_FormClosed(object sender, FormClosedEventArgs e)
 {
   this.Owner.Enabled = true;// Restore parent form to 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);
   fm.Show(this);// Set Parent Form 
   for (int i = 0; i < 100; i++)
   {
     fm.setPos(i);// Set the progress bar position 
     Thread.Sleep(100);// Sleep time is 100
   }
   fm.Close();// Close the form 
}

Add: A friend said in vs 2003 fm. Show (this): Is not supported, then you can add an extra parameter to the constructor of From2:


public Form OwnerForm;
public Form2(int _Minimum,int _Maximum,Form _OwnerForm)// Minimum and maximum values with parameters representing the range of the progress bar 
{
   InitializeComponent();
   progressBar1.Maximum=_Maximum;// Set the maximum value of the range 
   progressBar1.Value = progressBar1.Minimum = _Minimum;// Set the minimum value of the range 
   this.OwnerForm=_OwnerForm;
}
private void Form2_Load(object sender, EventArgs e)
{
   this.OwnerForm.Enabled = false;// Sets the parent form unavailable 
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
   this.OwnerForm.Enabled = true;// Restore parent form to available 
}

The corresponding modifications in Form1 are as follows:


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

For more readers interested in C # related content, please check out the topics on this site: "C # Common Control Usage Tutorial", "C # Form Operation Tips Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Tips Summary"

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


Related articles: