简体   繁体   English

进度栏C#未显示实际进度

[英]Progress Bar C# not showing the real progress

I am new to programming in C#.The program should convert kg tolbs/oz/grams. 我是C#编程的新手,该程序应将kg转换为kg / lb / oz / g。 The kilogram number is in the textBox1 and in the second textBox is the result.The ProgressBar maximum is 50001 and if i have , for exampe , 400 , it will show me the progressBar complete, even if it should be around 20%-30%. 公斤数在textBox1中,在第二个textBox中是结果。ProgressBar最大值为50001,如果我有,例如400,它将向我显示progressBar完成,即使它应该在20%-30%左右。 Thank you. 谢谢。

    private void button1_Click(object sender, EventArgs e)
    {
        int s = Convert.ToInt32(textBox1.Text);
        progressBar1.Maximum = 50001;
        if (s > 0 && s <= 500)
        {
            if (radioButton1.Checked == true)
            {
                progressBar1.Value = 0;
                int k = Convert.ToInt32(s * 2.20462262);
                textBox2.Text = Convert.ToString(k);
                for (int i = 1; i <= k; i++)
                {
                    progressBar1.Increment(i);
                }
            }
            if (radioButton2.Checked == true)
            {
                progressBar1.Value = 0;
                int k = Convert.ToInt32(s * 35.2739619);
                textBox2.Text = Convert.ToString(k);
                for (int i = 1; i <= k; i++)
                {
                    progressBar1.Increment(i);
                }
            }

            if (radioButton3.Checked == true)
            {
                progressBar1.Value = 0;
                int k = Convert.ToInt32(s * 1000);
                textBox2.Text = Convert.ToString(k);
                for (int i = 1; i <= k; i++)
                {
                    progressBar1.Increment(i);
                }
            }

        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        textBox1.Text = textBox2.Text = "";
        progressBar1.Value = 0;
    }
}

I guess the problem is that you're using progressBar.increment instead of setting the value property. 我想问题是您使用的是progressBar.increment而不是设置value属性。 Increment adds the given value to the current value. Increment将给定值添加到当前值。

so for example in this loop 所以在这个循环中

for (int i = 1; i <= k; i++)
{
  progressBar1.Increment(i);
}

For the first iteration (i = 1), the value of the progressbar will be 1, for the second iteration it will be 3 because you're incrementing 1 by 2 which equals to 3. For the 3 iteration it will be 6 ... 对于第一次迭代(i = 1),进度条的值为1,对于第二次迭代,它将为3,因为您将1加2等于3。对于3次迭代,它将为6 .. 。

So instead of using a loop, you could just wirte 因此,除了使用循环之外,您还可以

progressBar1.value = k

It's a bit hard to tell without more context, but my guess is that you never reset the progressBar1.Value back to 0 outside of the various if blocks. 如果没有更多上下文,很难说出来,但是我的猜测是,您永远不会在各种if块之外将progressBar1.Value重置为0 I'm assuming this is part of a winforms or wpf application, you need to notify the main gui thread that a control needs to be updated. 我假设这是winforms或wpf应用程序的一部分,您需要通知主GUI线程需要更新控件。 The way to do this is typically via INotifyPropertyChanged . 这样做的方法通常是通过INotifyPropertyChanged See here for some background. 有关某些背景,请参见此处

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

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