简体   繁体   English

如何让 WinForms 进度条在不可见时更新,然后以更新后的值显示?

[英]How do I get WinForms Progress Bar to update while not visible, and then show at the updated value?

I have a project where I have a UserControl that is a download status panel and has a progress bar in it.我有一个项目,其中有一个 UserControl,它是一个下载状态面板,其中有一个进度条。 The panel is hidden by default and shown when the user presses a button.默认情况下该面板是隐藏的,并在用户按下按钮时显示。 In the background the progress bar is being updated as the download progresses.在后台,进度条随着下载的进行而更新。

The Problem问题

When I show the download panel the progress bar starts from zero and then animates up to its actual value, giving the illusion that this progress just happened as you opened the panel.当我显示下载面板时,进度条从零开始,然后动画到其实际值,给人一种这种进度只是在您打开面板时发生的错觉。

How can I get it to just show where it is, instead of starting from zero and then progressing up.我怎样才能让它只显示它的位置,而不是从零开始然后进步。 I want the animation when the panel is open and progress is happening in real-time, but when I open the panel it should just start from where the current progress is.我想要在面板打开并且进度实时发生时的动画,但是当我打开面板时,它应该从当前进度的位置开始。

I have a simple demo project that has the following code that replications the issue.我有一个简单的演示项目,其中包含以下复制问题的代码。 Replication Steps are:复制步骤是:

  • Create a VS .NET WinForms project and add the following UserControl and Form.创建一个 VS .NET WinForms 项目并添加以下 UserControl 和 Form。
  • Run the project.运行项目。
  • Click on the "Set Progress to 50%" button.单击“将进度设置为 50%”按钮。
  • Click on the "Show Progress User Control" button.单击“显示进度用户控件”按钮。
  • Expected Behaviour: Progress bars shows at 50%.预期行为:进度条显示为 50%。
  • Actual Behaviour: Progress bar shows at 0% and then animates to 50%.实际行为:进度条显示为 0%,然后动画显示为 50%。

I have tried invalidating it using ProgressBar.Invalidate and then calling ProgressBar.Update but it makes no difference.我曾尝试使用 ProgressBar.Invalidate 使其无效,然后调用 ProgressBar.Update 但它没有区别。 I have searched all over the web but not seen this problem, all the problems I see are thread related, and this is not.我在网上搜索过但没有看到这个问题,我看到的所有问题都与线程有关,而这不是。

Any help would be much appreciated.任何帮助将非常感激。

UserControl用户控件

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        this.Size = new Size(351, 48);
        this.Visible = false;

        progressBar1 = new ProgressBar();
        progressBar1.Location = new System.Drawing.Point(15, 22);
        progressBar1.Name = "progressBar1";
        progressBar1.Size = new System.Drawing.Size(322, 5);
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 100;
        this.Controls.Add(progressBar1);
    }

    public void SetPogress(int progress)
    {
        progressBar1.Value = progress;
        progressBar1.Update();
    }
}

Form形式

public partial class Form1 : Form
{
    UserControl1 uc1 = null;

    public Form1()
    {
        InitializeComponent();

        // Add User Control with ProgressBar
        uc1 = new UserControl1();
        uc1.Location = new Point(50, 400);
        this.Controls.Add(uc1);

        // Add Set Progess Button
        var btn_SetProgress = new System.Windows.Forms.Button();
        btn_SetProgress.Location = new System.Drawing.Point(47, 46);
        btn_SetProgress.Name = "btn_SetProgress";
        btn_SetProgress.Size = new System.Drawing.Size(113, 23);
        btn_SetProgress.Text = "Set Progress to 50%";
        btn_SetProgress.Click += this.btn_SetProgress_Click;
        this.Controls.Add(btn_SetProgress);

        // Add Show Progess Button
        var btn_ShowProgress = new System.Windows.Forms.Button();
        btn_ShowProgress.Location = new System.Drawing.Point(47, 91);
        btn_ShowProgress.Name = "btn_ShowProgress";
        btn_ShowProgress.Size = new System.Drawing.Size(147, 23);
        btn_ShowProgress.Text = "Show Progress User Control";
        btn_ShowProgress.Click += this.btn_ShowProgress_Click;
        this.Controls.Add(btn_ShowProgress);
    }

    private void btn_SetProgress_Click(object sender, EventArgs e)
    {
        uc1.SetPogress(50);
    }

    private void btn_ShowProgress_Click(object sender, EventArgs e)
    {
        uc1.Visible = true;
    }
}

Thanks to those who responded.感谢那些回应的人。 I tried both suggested and the comment by @Jimi worked, so the final solution was to update the SetProgress method as follows:我尝试了@Jim 的建议和评论,所以最终的解决方案是更新 SetProgress 方法,如下所示:

    public void SetPogress(int progress)
    {
        if (!progressBar1.IsHandleCreated) { var h = progressBar1.Handle; }
        progressBar1.Value = progress;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM