Mar*_*ler 71 c# winforms progress-bar
我正在使用C#和Windows Forms.我有一个正常的进度条在该程序中正常工作,但现在我有另一个操作,其中持续时间不容易计算.我想显示一个进度条,但不知道启动/停止滚动选框的最佳方法.我希望有一些简单的东西,如设置选取框速度,然后有一个start()和stop(),但它看起来并不那么简单.我必须在后台运行一个空循环吗?我该如何做到最好?谢谢
Pau*_*her 107
使用样式设置为的进度条Marquee.这代表了一个不确定的进度条.
myProgressBar.Style = ProgressBarStyle.Marquee;
Run Code Online (Sandbox Code Playgroud)
您还可以使用该MarqueeAnimationSpeed属性设置在您的进度条上为小块颜色设置动画所需的时间.
小智 52
要开始/停止动画,您应该这样做:
开始:
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 30;
Run Code Online (Sandbox Code Playgroud)
停止:
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.MarqueeAnimationSpeed = 0;
Run Code Online (Sandbox Code Playgroud)
此代码是登录表单的一部分,用户在等待身份验证服务器响应.
using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
namespace LoginWithProgressBar
{
public partial class TheForm : Form
{
// BackgroundWorker object deals with the long running task
private readonly BackgroundWorker _bw = new BackgroundWorker();
public TheForm()
{
InitializeComponent();
// set MarqueeAnimationSpeed
progressBar.MarqueeAnimationSpeed = 30;
// set Visible false before you start long running task
progressBar.Visible = false;
_bw.DoWork += Login;
_bw.RunWorkerCompleted += BwRunWorkerCompleted;
}
private void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// hide the progress bar when the long running process finishes
progressBar.Hide();
}
private static void Login(object sender, DoWorkEventArgs doWorkEventArgs)
{
// emulate long (3 seconds) running task
Thread.Sleep(3000);
}
private void ButtonLoginClick(object sender, EventArgs e)
{
// show the progress bar when the associated event fires (here, a button click)
progressBar.Show();
// start the long running task async
_bw.RunWorkerAsync();
}
}
}
Run Code Online (Sandbox Code Playgroud)