This article mainly introduces the implementation of the C # Band percentage of the progress bar function, analysis of the functional requirements with a percentage of the progress bar and in combination with examples of specific implementation steps and related operations, the need for friends can refer to the following
This example describes the C # implementation of the progress bar feature with percentages. Share to everyone for your reference, as follows:
Functional Requirements:
If a time-consuming calculation process is performed in the program, I want to pop a progress bar window after the user taps the button, showing the progress being performed (preferably with a percentage), and when the execution is complete, the progress bar window closes and goes back to the main program window. The parent form cannot click Actions until the child window is closed.
Implementation method:
First design the Form2 progress bar form, put 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, the minimum and maximum values of 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;//Set the progress value
label1.Text = (value * 100 / progressBar1.Maximum).ToString() + "%";//Display percentage
}
Application.DoEvents();//Key point, must be added, otherwise the parent and child forms will be suspended
}
private void Form2_Load(object sender, EventArgs e)
{
this.Owner.Enabled = false;//Set the parent form to be unavailable
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Owner.Enabled = true;//Reply 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);
fm.Show(this);//Set the parent form
for (int i = 0; i <100; i++)
{
fm.setPos(i);//Set the position of the progress bar
Thread.Sleep(100);//Sleep time is 100
}
fm.Close();//Close the form
}
add: Some friends say FM in VS2003. Show (This): is unsupported, you can add a parameter to the From2 constructor:
public Form OwnerForm;
public Form2(int _Minimum,int _Maximum,Form _OwnerForm)//With parameters, the minimum and maximum values of 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;//Set the parent form to be unavailable
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
this.OwnerForm.Enabled = true;//Reply the parent form is available
}
The corresponding changes in Form1 are:
private void button1_Click(object sender, EventArgs e)
{
Form2 fm = new Form2(0,100,this);
fm.Show();//Set the parent form
for (int i = 0; i <100; i++)
{
fm.setPos(i);//Set the position of the progress bar
Thread.Sleep(100);//Sleep time is 100
}
fm.Close();//Close the form
}