当前位置:网站首页>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
边栏推荐
- golang八股文整理(持续搬运)
- The importance of strategic offensive capability is much higher than strategic defensive capability
- 365-day challenge LeetCode1000 questions - Day 044 Maximum element in the layer and level traversal
- 如何使用StarUML画类图[通俗易懂]
- What should I do if selenium is reversed?
- 五种数据提交方式的优化
- SAP 电商云 Spartacus SSR Optimization Engine 几处 timeout 的执行顺序
- CWE4.8 -- 2022年危害最大的25种软件安全问题
- AMBA APB学习记录(AMBA 2.0)
- golang中使用泛型
猜你喜欢
![[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)](/img/a6/8d53f5087a33c2bea8c99a5bb922da.png)
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节

NameNode (NN) 和SecondaryNameNode (2NN)工作机制

C#使用NumericUpDown控件

PyQt5 rapid development and actual combat 10.2 compound interest calculation && 10.3 refresh blog clicks

ASM module in SAP Ecommerce Cloud Spartacus UI and Accelerator UI

PyQt5快速开发与实战10.2 复利计算 && 10.3 刷新博客点击量

Network layer key protocol - IP protocol

Invalid bound statement (not found)出现的原因和解决方法
随机推荐
networkx绘制度分布
跨境电商小知识之跨境电商物流定义以及方式讲解
报错:npm ERR code EPERM
Network layer key protocol - IP protocol
五种数据提交方式的优化
Sliding window method to segment data
查看Mysql数据库版本
PartImageNet物体部件分割(Semantic Part Segmentation)数据集介绍
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
Invalid bound statement (not found)出现的原因和解决方法
/run/NetworkManager占用空间过大
NameNode (NN) and SecondaryNameNode (2NN) working mechanism
求一份常见Oracle故障模拟场景
NameNode故障处理的两种方法
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
Two methods of NameNode failure handling
The cluster of safe mode
Flutter keyboard visibility
集群中增加数据节点与退役数据节点
中望3D 2023正式发布,设计仿真制造一体化缩短产品开发周期