【问题标题】:Incrementing Text in Circular Progress Bar在循环进度条中增加文本
【发布时间】:2017-10-01 18:06:59
【问题描述】:
private void _btnOK_Click(object sender, EventArgs e)
    {
        _label1.Hide();
        _label2.Hide();
        _label3.Hide();

        for(int i = 1; i <= 100; i++)
        {
            Thread.Sleep(5);
            _circularprogressbar.Value = i;
            _circularprogressbar.Update();
        }
    }

    private void LoadingScreen_Load(object sender, EventArgs e)
    {
        _circularprogressbar.Value = 0;
        _circularprogressbar.Minimum = 0;
        _circularprogressbar.Maximum = 100;
    }
}

}

这是我的代码。我想做的是,我想在进度条内有一个文本,显示从 1% 到 100% 的进度百分比。 我可以在我的代码中添加什么? 谢谢

【问题讨论】:

    标签: c# visual-studio progress-bar


    【解决方案1】:

    这是我要做的:

    private void _btnOK_Click(object sender, EventArgs e)
        {
            _label1.Hide();
            _label2.Hide();
            _label3.Hide();
    
            for(int i = 1; i <= 100; i++)
            {
                _circularprogressbar.Value = i;
                _percent_lable_name.Text = string.Format("{0}%", _circularprogressbar.Value);
                _circularprogressbar.Update();
            }
        }
    
        private void LoadingScreen_Load(object sender, EventArgs e)
        {
            _circularprogressbar.Value = 0;
            _circularprogressbar.Minimum = 0;
            _circularprogressbar.Maximum = 100;
        }
    }
    

    看看对你有没有帮助!

    谢谢

    Techcraft7 :)

    【讨论】:

      【解决方案2】:

      Thread.Sleep(5) 阻塞了你的整个 UI 线程。如果您想让您的 UI 响应式,在进度发生时,您需要为它创建一个单独的线程。像这样的:

      private void _btnOK_Click(object sender, EventArgs e)
      {
          _label1.Hide();
          _label2.Hide();
          _label3.Hide();
      
          Task.Factory.StartNew(() => 
          {
              for (int i = 1; i <= 100; i++)
              {
                  Thread.Sleep(5);
                  Invoke((Action)(() =>
                  {
                      _circularprogressbar.Value = i;
                      _circularprogressbar.Update();
                  }));
              }
          });
      }
      

      请注意,您不需要使用 Invoke 来 BeginInvoke 从该线程内部访问 UI 组件。

      【讨论】:

      • 调用是为了什么?
      • 在 UI 线程中执行东西。如果您尝试从另一个线程处理 UI 对象,您将遇到跨线程冲突。实际上,访问线程外的任何内容都必须谨慎,并且可能会导致问题。
      猜你喜欢
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多