[Yugong Series] Detailed explanation of ProgressBar control on Winform control special topic in September 2023


Preface

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed explanation of ProgressBar control

The ProgressBar control is one of the most commonly used controls in Winform. It is usually used to represent the status of an operation progress. The ProgressBar control can display horizontal or vertical progress bars, progress points, or animations.

The following are some common properties and methods for operating the ProgressBar control:

Attributes:

  1. Value: Indicates the current progress value of ProgressBar.
  2. Maximum: Indicates the maximum value of ProgressBar.
  3. Minimum: Represents the minimum value of ProgressBar.

method:

  1. PerformStep(): Each time this method is called, the progress value of the ProgressBar will increase by a certain value (default is 1).
  2. Increment(): Each time this method is called, the progress value of the ProgressBar will increase by a certain value (can be specified through the parameters of the Increment method).

Here is some sample code using the ProgressBar control:

// 初始化ProgressBar控件
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Step = 1;
progressBar1.Value = 0;

// 更新ProgressBar进度值
private void UpdateProgress()
{
    
    
    if (progressBar1.Value < progressBar1.Maximum)
    {
    
    
        progressBar1.PerformStep();
    }
}

// 重置ProgressBar进度值
private void ResetProgress()
{
    
    
    progressBar1.Value = 0;
}

// 设置ProgressBar进度值
private void SetProgress(int value)
{
    
    
    progressBar1.Value = value;
}

// 增加ProgressBar进度值
private void IncreaseProgress(int value)
{
    
    
    progressBar1.Increment(value);
}

Through the above code, the basic progress bar function of the ProgressBar control can be realized.

1. Introduction to attributes

1.1 MarqueeAnimationSpeed

The MarqueeAnimationSpeed ​​property of the ProgressBar control is an integer property used to set the scrolling speed of the progress bar in animation mode, in milliseconds. When the Style property of the ProgressBar control is set to Marquee, the control will display the progress bar in an animated manner. At this time, the MarqueeAnimationSpeed ​​property can set the speed of the progress bar scrolling.

The following is sample code using the MarqueeAnimationSpeed ​​property:

// 设置ProgressBar控件为动画模式
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 50; // 设置滚动速度为50毫秒

// 取消ProgressBar控件的动画模式
progressBar1.Style = ProgressBarStyle.Continuous;

In the above code, first set the Style property of the ProgressBar control to Marquee, and then set the MarqueeAnimationSpeed ​​property to 50, which means that the progress bar scrolls every 50 milliseconds. Finally, you can cancel the display of animation mode by setting the Style property of the ProgressBar control to Continuous.

It should be noted that in animation mode, the Value property of the ProgressBar control does not work, because in animation mode, the progress bar is displayed in an automatic circular scrolling manner and the specific progress value cannot be displayed. Therefore, when using animation mode, you should consider setting the Text property of the ProgressBar control to some relevant prompt information so that users can understand the status of the current operation.

2. Common scenarios

The ProgressBar control is one of the commonly used controls in Winform. It is often used in the following scenarios:

  1. Display task progress: The ProgressBar control can visually display the progress of the task and provide users with visual feedback.

  2. Indicates file download progress: During the file download process, we usually use the ProgressBar control to display the download progress and give the user an indication of the download progress.

  3. Indicates system resource occupancy: Some applications need to run for a long time and may occupy more system resources. At this time, using the ProgressBar control can let users understand the running status of the program, thereby improving the stability and reliability of the program.

  4. Indicates the progress of data loading: In applications with large amounts of data, data loading may take a certain amount of time. Using the ProgressBar control allows users to clearly understand the progress of data loading and improves user experience.

The ProgressBar control can be used to display any scene that needs to show progress, giving users an intuitive feedback and increasing the operability and friendliness of the program.

3. Specific cases

The following is a complete case of the ProgressBar control in Winform, which demonstrates how to use the ProgressBar control to display progress:

  1. Create a new Winform application and add a ProgressBar control to the form.

  2. In the code, first set the minimum and maximum values ​​of the ProgressBar:

progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
  1. Next, write a method to update the ProgressBar value. In this example we will use a timer to simulate an increase in progress:
private void timer1_Tick(object sender, EventArgs e)
{
    
    
    if (progressBar1.Value < progressBar1.Maximum)
    {
    
    
        progressBar1.Value++;
        label1.Text = progressBar1.Value.ToString() + "%";
    }
    else
    {
    
    
        timer1.Stop();
        MessageBox.Show("进度完成!");
    }
}

In this method, we check if the current progress is less than the maximum value, and if so, we increase the value of the ProgressBar and then update a label to show the current progress percentage. If the progress reaches the maximum value, we stop the timer and display a message box to notify the user that the task is completed.

  1. Finally, start the timer in the form's Load event:
private void Form1_Load(object sender, EventArgs e)
{
    
    
    timer1.Start();
}

The complete code is as follows:

public partial class Form1 : Form
{
    
    
    public Form1()
    {
    
    
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    
    
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 100;

        timer1.Interval = 100;
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
    
    
        if (progressBar1.Value < progressBar1.Maximum)
        {
    
    
            progressBar1.Value++;
            label1.Text = progressBar1.Value.ToString() + "%";
        }
        else
        {
    
    
            timer1.Stop();
            MessageBox.Show("进度完成!");
        }
    }
}

Insert image description here

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132799166