代码之家  ›  专栏  ›  技术社区  ›  Mark Stahler

Windows窗体进度栏:启动/停止字幕的最简单方法?

  •  66
  • Mark Stahler  · 技术社区  · 16 年前

    我使用的是C和Windows窗体。我有一个正常的进度条,在程序中运行良好,但现在我有另一个操作,无法轻松计算持续时间。我想显示一个进度条,但不知道启动/停止滚动字幕的最佳方法。我希望能做些简单的事情,比如设置字幕速度,然后开始(start)和停止(stop)),但看起来并不那么简单。我必须在后台运行空循环吗?我该怎么做?谢谢

    7 回复  |  直到 8 年前
        1
  •  99
  •   Paul Fisher    16 年前

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

    myProgressBar.Style = ProgressBarStyle.Marquee;
    

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

        2
  •  51
  •   Mo Patel Mahdi Tahsildari    11 年前

    要启动/停止动画,应执行以下操作:

    开始:

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

    停止:

    progressBar1.Style = ProgressBarStyle.Continuous;
    progressBar1.MarqueeAnimationSpeed = 0;
    
        3
  •  9
  •   Hans Passant    16 年前

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

        4
  •  8
  •   Arda Basoglu    13 年前

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

    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();
            }
        }
    }    
    
        5
  •  2
  •   tvanfosson    16 年前

    有一个不错的 article 在msdn上有关于这个主题的代码。我假设将Style属性设置为ProgressBarStyle.Marquee是不合适的(或者这是您试图控制的??——我不认为可以停止/启动这个动画,尽管你可以控制速度,如@paul所示)。

        6
  •  1
  •   Nameless One    9 年前

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

        7
  •  -3
  •   Asher    16 年前

    你可以使用 Timer (system.windows.forms.timer)。

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

    …就像Windows资源管理器一样:-)