简体   繁体   English

Winforms进度栏无法正常工作

[英]Winforms Progress bar is not working correctly

This is my first time with progress bar.I am not able to see the progress indication in my progress bar. 这是我第一次使用进度条。我无法在进度条中看到进度指示。 I have written the following code. 我写了下面的代码。

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Maximum = 1000000;
            progressBar1.Value = 0;
            progressBar1.Step=10;
            Int64 i = 10000000000;
            while (i != 1)
            {
                i = i / 10;
                System.Threading.Thread.Sleep(1000);
                progressBar1.Increment(10);
            }
        }
    }
}

I dont see any progress shown in my progress bar. 我在进度栏中看不到任何进度。 Please give me a solution 请给我一个解决方案

Try maximum of 100 ;) 尝试最多100;)

With / 10 you dont make 100000 (1000000 / step 10) runs. 使用/ 10,您不会使100000(1000000 /步骤10)运行。 you will make 10 ;) 你会赚10;)

Your basic use of the progress bar is correct, but there is something weird about some of your other values. 您对进度条的基本使用是正确的,但是其他一些值有些奇怪。 In fact, your code will work partly, but the loop will complete before completing the progress bar (my quick calculations indicate that it would take around 28 minutes to complete filling the bar even if your loop continued though! ;) ) 实际上,您的代码将部分起作用,但是循环将在完成进度条之前完成(我的快速计算表明,即使循环继续进行,也需要大约28分钟才能完成填充条!;))

In other words, you probably simply did not see the change in the progress bar because it was so small! 换句话说,您可能根本看不到进度栏中的更改,因为它是如此之小!

A little modification may improve the example a little, and show the progress bar working as intended (and a little quicker than your original code). 稍加修改可能会改善示例,并显示进度条按预期方式工作(并且比原始代码快一点)。

    private void button1_Click(object sender, EventArgs e)
    {
        progressBar1.Maximum = 10; // Smaller number of steps needed
        progressBar1.Value = 0;
        progressBar1.Step = 1;
        Int64 i = 10000000000; 
        while (i != 1) // This will require 10 iterations 
        {
            i = i / 10;
            System.Threading.Thread.Sleep(1000); 
            progressBar1.Increment(1); // one step at a time
        }
    }

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

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