如何在c#中完成进度条时弹出一个窗口

时间:2017-12-24 23:27:09

标签: c#

我已经看到了类似的答案,但它不在c#中,所以我决定提出这个问题。 https://gyazo.com/3ff6efd90fa390cd1f071b693027fcd3到达那一点后,我想要一个弹出的窗口,上面写着"已成功加载..."。如果有帮助,我设置的定时器间隔为50。 这是代码:

private void button1_Click(object sender, EventArgs e)
    {
        this.timer1.Start();
    }

private void progressBar1_Click(object sender, EventArgs e)
    {

    }

private void timer1_Tick(object sender, EventArgs e)
    {
        this.progressBar1.Increment(1);
    }

2 个答案:

答案 0 :(得分:1)

考虑改变:

private void timer1_Tick(object sender, EventArgs e)
    {
        this.progressBar1.Increment(1);
    }

为:

private void timer1_Tick(object sender, EventArgs e)
{
    var before = this.progressBar1.Value;
    this.progressBar1.Increment(1);
    var after = this.progressBar1.Value;
    if (after > before && after == this.progressBar1.Maximum)
    {
        MessageBox.Show("Successfully loaded...");
    }
}

通过检查值是否更改表示当前值为Maximum,您知道进度条已完成。

答案 1 :(得分:0)

执行某个步骤后,如果Value已达到Maximum,只需检查您的代码:

progressBar.PerformStep();

if (progressBar.Value == progressBar.Maximum)
    MessageBox.Show("Successfully loaded...");

使用BackgroundWorker实例:

BackgroundWorker bgw = new BackgroudWorker();
bgw.DoWork += bgw_DoWork;
bgw.ProgressChanged += bgw_ProgressChanged;
bgw.RunWorkerCompleted += bgw_RunWorkerCompleted;
bgw.WorkerReportsProgress = true;
bgw.RunWorkerAsync();

private void bgw_DoWork(Object sender, DoWorkEventArgs e)
{
    Int32 total = 147;

    for (Int32 i = 0; i < total; ++i)
    {
        Int32 progress = (i * 100) / total;
        bgw.ReportProgress(progress, i);
    }
}

private void bgw_ProgressChanged(Object sender, ProgressChangedEventArgs e)
{
    progressBar.Value = e.ProgressPercentage;
}

private void bgw_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
{
    MessageBox.Show("Successfully loaded...");
}

修改

由于问题发生了变化,提供了代码剪切,这里有一个更新的答案(顺便提一下,这反映了我的第一个提案):

private void timer1_Tick(object sender, EventArgs e)
{
    this.progressBar1.Increment(1);

    if (this.progressBar1.Value == this.progressBar1.Maximum)
    {
        //...

        MessageBox.Show("Successfully loaded...");
    }
}