C# Windows 窗体 ProgressBar:启动/停止选取框的最简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/312936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:44:36  来源:igfitidea点击:

Windows Forms ProgressBar: Easiest way to start/stop marquee?

c#winformsprogress-bar

提问by Mark Stahler

I am using C# and Windows Forms. I have a normal progress bar working fine in the program, but now I have another operation where the duration cannot be easily calculated. I would like to display a progress bar but don't know the best way to start/stop the scrolling marquee. I was hoping for something as simple as setting the marquee speed and then having a start() and stop() but it doesn't appear to be that simple. Do I have to run an empty loop in the background? How do I best do this? Thanks

我正在使用 C# 和 Windows 窗体。我有一个正常的进度条在程序中工作正常,但现在我有另一个操作,无法轻松计算持续时间。我想显示一个进度条,但不知道启动/停止滚动字幕的最佳方式。我希望有一些简单的事情,比如设置跑马灯速度,然后有一个 start() 和 stop(),但它似乎并不那么简单。我是否必须在后台运行一个空循环?我如何最好地做到这一点?谢谢

采纳答案by Paul Fisher

Use a progress bar with the style set to Marquee. This represents an indeterminate progress bar.

使用样式设置为 的进度条Marquee。这代表一个不确定的进度条。

myProgressBar.Style = ProgressBarStyle.Marquee;

You can also use the MarqueeAnimationSpeedproperty to set how long it will take the little block of color to animate across your progress bar.

您还可以使用该MarqueeAnimationSpeed属性来设置小块颜色在进度条上设置动画所需的时间。

回答by Asher

you can use a Timer(System.Windows.Forms.Timer).

您可以使用计时器(System.Windows.Forms.Timer)。

Hook it's Tick event, advance then progress bar until it reaches the max value. when it does (hit the max) and you didn't finish the job, reset the progress bar value back to minimum.

钩住它的 Tick 事件,前进然后进度条,直到达到最大值。当它(达到最大值)并且您没有完成工作时,将进度条值重置回最小值。

...just like Windows Explorer :-)

...就像 Windows 资源管理器 :-)

回答by tvanfosson

There's a nice articlewith code on this topic on MSDN. I'm assuming that setting the Style property to ProgressBarStyle.Marquee is not appropriate (or is that what you are trying to control?? -- I don't think it is possible to stop/start this animation although you can control the speed as @Paul indicates).

MSDN 上有一篇很好的文章,其中包含有关此主题的代码。我假设将 Style 属性设置为 ProgressBarStyle.Marquee 是不合适的(或者这是您要控制的内容??-尽管您可以控制速度,但我认为无法停止/启动此动画正如@Paul 指出的那样)。

回答by Hans Passant

It's not how they work. You "start" a marquee style progress bar by making it visible, you stop it by hiding it. You could change the Style property.

这不是他们的工作方式。您可以通过使其可见来“启动”一个选取框样式的进度条,通过隐藏它来停止它。您可以更改 Style 属性。

回答by Hans Passant

To start/stop the animation, you should do this:

要开始/停止动画,您应该这样做:

To start:

开始:

progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 30;

To stop:

停止:

progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.MarqueeAnimationSpeed = 0;

回答by Arda Basoglu

This code is a part of a login form where the users wait for the authentication server to respond.

此代码是用户等待身份验证服务器响应的登录表单的一部分。

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();
        }
    }
}    

回答by Nameless One

Many good answers here already, although you also need to keep in mind that if you are doing long-running processing on the UI thread (generally a bad idea), then you won't see the marquee moving either.

这里已经有很多好的答案,但您还需要记住,如果您在 UI 线程上进行长时间运行的处理(通常是一个坏主意),那么您也不会看到选取框移动。