C# 如何设置进度条的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13440098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:33:33  来源:igfitidea点击:

How to set value of progress bar

c#winformsprogress-bar

提问by user1770370

I have written a user control using C# Winforms. In the user control, I have three textboxes:

我已经使用 C# Winforms 编写了一个用户控件。在用户控件中,我有三个文本框:

  • txtStartNumber - input is of type: int.
  • txtEndNumber - input is of type: int.
  • txtQuantity - iput is of type: int. (value = txtEndNumber - txtStartNumber)
  • txtStartNumber - 输入类型:int。
  • txtEndNumber - 输入类型:int。
  • txtQuantity - iput 的类型:int。(值 = txtEndNumber - txtStartNumber)

The progress bar denotes the no. of records added to the database and its total range is set to be equal to txtQuantity.

进度条表示没有。添加到数据库中的记录数,其总范围设置为等于 txtQuantity。

When one or more records are duplicate, the progress bar is stopped.

当一个或多个记录重复时,进度条停止。

My questions are:

我的问题是:

  1. How to set the initial value of the progress bar?
  2. How to manage the progress shown by progress bar?
  1. 如何设置进度条的初始值?
  2. 如何管理进度条显示的进度?

How I save it to the database:

我如何将其保存到数据库中:

for (long i = from; i < to; i++)
{
    for (int j = 0; j < (to - from); j++)
    {
        arrCardNum[j] = from + j;
        string r = arrCardNum[j].ToString();
        try
        {
            sp.SaveCards(r, 2, card_Type_ID, SaveDate, 2);
            progressBar1.Value = j;
        }
    }
}

采纳答案by Zaheer Ahmed

Try this:

尝试这个:

private void StartBackgroundWork() {
    if (Application.RenderWithVisualStyles)
        progressBar.Style = ProgressBarStyle.Marquee;
    else {
        progressBar.Style = ProgressBarStyle.Continuous;
        progressBar.Maximum = 100;
        progressBar.Value = 0;
        timer.Enabled = true;
    }
    backgroundWorker.RunWorkerAsync();
}

private void timer_Tick(object sender, EventArgs e) {
    progressBar.Value += 5;
    if (progressBar.Value > 120)
        progressBar.Value = 0;
}

The Marquee style requires VisualStyles to be enabled, but it continuously scrolls on its own without needing to be updated. I use that for database operations that don't report their progress.

Marquee 样式需要启用 VisualStyles,但它会自行连续滚动而无需更新。我将它用于不报告进度的数据库操作。

Here is another Progress Bar Tutorial

这是另一个进度条教程

回答by user3428180

You can't use loop to do this with progressbar. There is a difference between running code in for, while, do...while loops or in timers. In loops code is immediately done and you can't see this, in timers you can. Even if you try to put in loops if counters, it will not works:

您不能使用循环来使用进度条执行此操作。在 for、while、do...while 循环或计时器中运行代码之间存在差异。在循环中代码会立即完成,您看不到这一点,在计时器中您可以看到。即使您尝试将 if counters 放入循环中,它也不会起作用:

for(int i=a;i<b;++i)
{
    if (cnt < 1000000)
    {
        IncrProgressBar();
        cnt++;
    }
    else
    {
        cnt = 0;
    }
}

If you want to use progressbar to do this then you must put in timer OnTick event code that adds data to database, and in this event increment progressbar value. It's similarly with changing form component's other properties (Text, Size, ...). If you want to see change on component you must use timers.

如果您想使用进度条来执行此操作,那么您必须输入将数据添加到数据库的计时器 OnTick 事件代码,并在此事件中增加进度条值。与更改表单组件的其他属性(文本、大小等)类似。如果您想查看组件的变化,您必须使用计时器。

回答by The C Shaper

To change the value use:

要更改值使用:

private void timer1_Tick(object sender, EventArgs e)
    {
        progressBar2.Value = progressBar2.Value - 15;
    }

In C#

在 C# 中