当前位置:网站首页>C# control ToolStripProgressBar usage
C# control ToolStripProgressBar usage
2022-07-31 13:16:00 【dusk and starry sky】
C# ToolStripProgressBar usage analysis
It takes a long time in the process of processing, so I want to add a progress bar at the bottom of the form to indicate the progress of the processing process, I found that there is a magical control such as ToolStripProgressBar, which can be convenientUsed to reflect progress.But for how to use it, the first contact is quite confusing, so I habitually went to MSDN to find the official documentation.Sure enough, the official document gives a detailed routine, and the link is posted here: MSDN:
ToolStripProgressBar.
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.toolstripprogressbar?redirectedfrom=MSDN&view=windowsdesktop-6.0
How to use
The ProgressBar control needs to be used in conjunction with a Class named BackgroundWorker.This class is a sub-thread that can work independently of the main thread. As long as the action that needs to be indicated by the progress bar is bound to this class, and the progress value of the progress bar is rewritten in real time according to the degree of completion, the progress bar can be realized.Synchronized display.It should be noted here that in order to reflect the progress of the work in real time, the WorkerReportsProgress property of the BackgroundWorker needs to be changed to True.
Mainly need to bind the following three events to BackgroundWorker:
The action to start work: DoWork
The action to complete the work: RunWorkerCompleted
The action to refresh during work: ProgressChanged
First, let's look at how to bind events, this part can be bound in the view designerIt can also be manually bound in the implemented code. Here is an example of manual binding in the code:
private void InitializeBackgroundWorker()
{
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(
backgroundWorker1_ProgressChanged);
}
1
2
3
4
5
6
7
8
Start work DoWork
The function indicated by the progress bar needs to be bound here. It should be noted that the function needs to have two parameters for BackgroundWorker control. The worker parameter is used to restore the BackgroundWorker object, and the e parameter can indicate that the event that the action is completed isHow it is triggered (user canceled or action completed).Therefore, the function definition format should be as follows:
private void DoSomething(BackgroundWorker worker, DoWorkEventArgs e)
1
Two things need to be done in this action event: first, restore the worker; second, call the DoSomething function to start working.The code reference is as follows:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Restore worker object
BackgroundWorker worker = sender as BackgroundWorker;
//Start work
DoSomething(worker, e);
}
1
2
3
4
5
6
7
Finish the job RunWorkerCompleted
This is used to indicate how to do the next action when the job is finished.And you can distinguish whether the work is aborted or completed according to the incoming parameter e, so as to perform different "finishing" work.The code reference is as follows:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Restore the Worker object
BackgroundWorker worker = sender as BackgroundWorker;
//Determine whether an error caused an unexpected abort
if (e.Error != null)
{
//If an error occurs, the pop-up window will display the error message
MessageBox.Show(e.Error.Message);
}
//Determine whether the user cancels manually,If the program wants to support this function, it needs to have a cancel action in the program, and set e.cancel to true in this action
else if (e.Cancelled)
{
//Add the action that the user cancels manually
MessageBox.Show(“Canceled”);
}
//Judging whether it ends normally
else
{
//Add the closing action after the normal end
MessageBox.Show(“Complete”);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Refresh ProgressChanged at work
For the goal of adding a progress bar,Here, just update the progress value of the progress bar.Content that the user needs to refresh in real time during the process can also be added here.The code example is as follows:
private void backgroundWorker1_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
//Update the progress value of the progress bar
this.toolStripProgressBar1.Value = e.ProgressPercentage;
}
1
2
3
4
5
Summary
Case Download
https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?redirectedfrom=MSDN&view=net-6.0
边栏推荐
- 聊聊 SAP 产品 UI 上的消息显示机制
- 报错:npm ERR code EPERM
- IDEA找不到Database解决方法
- 365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
- 365天挑战LeetCode1000题——Day 044 最大层内元素和 层次遍历
- ECCV2022: Recursion on Transformer without adding parameters and less computation!
- ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI
- 深圳某游戏研发公司每个工位都装监控,网友:堪比“坐牢”!
- 网络协议及相关技术详解
- The operator,
猜你喜欢
随机推荐
centos7安装mysql5.7步骤(图解版)
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)
Two methods of NameNode failure handling
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
【CPU设计实战】简单流水线CPU设计
PyQt5 rapid development and actual combat 10.2 compound interest calculation && 10.3 refresh blog clicks
Adding data nodes and decommissioning data nodes in the cluster
How to quickly split and merge cell data in Excel
JSP response对象简介说明
文本相似度计算(中英文)详解实战
IDEA的database使用教程(使用mysql数据库)
基于高阶微分器的无模型滑模控制器及其在自动电压调节器中的应用
Detailed explanation of network protocols and related technologies
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
Network layer key protocol - IP protocol
Sliding window method to segment data
Batch大小不一定是2的n次幂!ML资深学者最新结论
Optimization of five data submission methods
Introduction to using NPM
Anaconda安装labelImg图像标注软件