今天就跟大家聊聊有关C#中怎么实现一个进度条效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
using System; using System.Collections.Generic; //C#进度条实现实例using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace 进度条2 //C#进度条实现实例{ public partial class Form1 : Form { private BackgroundWorker worker = new BackgroundWorker(); int N = 0; public Form1() { InitializeComponent(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; worker.DoWork += new DoWorkEventHandler(DoWork); worker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork); } public void DoWork( object sender, DoWorkEventArgs e) { e.Result = ComputeFibonacci(worker, e); } public void ProgessChanged( object sender, ProgressChangedEventArgs e) { progressBarX1.Value = e.ProgressPercentage; int V =(int)( e.ProgressPercentage / N); progressBarX1.Text = Convert.ToString(V) + "%"; } //C#进度条实现实例public void CompleteWork( object sender, RunWorkerCompletedEventArgs e) { progressBarX1.Text = "处理完毕!"; } private int ComputeFibonacci( object sender, DoWorkEventArgs e) { for (int i = 0; i <= 92800; i++) { if (worker.CancellationPending) { e.Cancel = true; return -1; } else { //C#进度条实现实例 worker.ReportProgress(i); //引发ProgessChanged事件 } } return -1; } private void btnStart_Click( object sender, EventArgs e) { N = 92800 / 100; progressBarX1.Maximum = 92800; worker.RunWorkerAsync(); //开始执行后台操作 } private void btnPause_Click( object sender, EventArgs e) { worker.CancelAsync(); //请求暂停后台操作 } } //C#进度条实现实例 }
看完上述内容,你们对C#中怎么实现一个进度条效果有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。