如何将进度条的最大值更改为用户在文本框中输入的数字? C#

时间:2016-02-08 03:13:14

标签: c# progress-bar

如何将进度条的最大值设置为用户在Windows应用程序表单上的文本框中输入的值?这是我目前的代码。我需要最大值为输入值。我还需要它来打印当前的时间和日期。

private void btnProgNum_Click(object sender, EventArgs e)
{                     
    progBarNum.Maximum = 10;
    backgroundWorker1.WorkerReportsProgress = true;
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    int i = 1;
    int endProg = Convert.ToInt32(txtNumProg.Text);
    while (i <= endProg )
    {
        System.Threading.Thread.Sleep(500);
        backgroundWorker1.ReportProgress(i);
        i++;
    }
}

private void backgroundWorker1_ProgressChanged_1(object sender, ProgressChangedEventArgs e)
{
    progBarNum.Value = e.ProgressPercentage;
        lblOutProg.Text = DateTime.Now.ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

        private void btnProgNum_Click(object sender, EventArgs e)
        {
        int n = int.Parse(txtNumProg.Text);
        int counter = 0;
        while (counter != n + 1)
        {
            counter++;
        }
            progBarNum.Maximum = n;
            backgroundWorker1.RunWorkerAsync();
            backgroundWorker1.WorkerReportsProgress = true;

       }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        int i = 1;
        int n = int.Parse(txtNumProg.Text);
        while (i <= n )
        {
            System.Threading.Thread.Sleep(500);
            backgroundWorker1.ReportProgress(i);
            i++;
         }
    }


    private void backgroundWorker1_ProgressChanged_1(object sender, ProgressChangedEventArgs e)
    {
    progBarNum.Value = e.ProgressPercentage;
        lblOutProg.Text = DateTime.Now.ToString();
    }

这是我修正问题的更新,其中传递了进度条的新最大值。现在我不认为我需要第二个while循环。它以这种方式工作,但似乎只需要一个while循环。