C#Winforms-ProgressBar无法正确显示和重置 [英] C# Winforms - ProgressBar does not display and reset properly

查看:55
本文介绍了C#Winforms-ProgressBar无法正确显示和重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows桌面应用程序,该应用程序是在Visual Basic中使用Windows窗体编写的.在此应用程序中,我想显示一个简单的进度栏,但遇到一个奇怪的问题.下面是一个简单的for循环示例,用于更新进度条:

I have a windows desktop application that I am writing using Windows Forms in visual basic. In this app I would like to display a simple progress bar, but I have come across a strange issue. The below is an example of a simple for loop which updates the progress bar:

pBar.Visible = true;
// pBar.Minimum and pBar.Step are both set to 1 using the properties pane in the design page
pBar.Maximum = 100;
for (int j = 0; j < 100; j++) {
    double pow = Math.Pow(j, j); //Calculation
    pBar.PerformStep();
}

上面的方法工作正常,但是,我希望能够重置进度条,以便以后再次使用它.我以为我可以在for循环之后简单地设置 pBar.Value = 1 ,但是奇怪的是,这破坏了进度条.该条完全没有填充,没有像以前那样稳定地增加.如果设置 pBar.Value = 50 ,则条形图将从中点开始,并且不会移动.它的行为就像在for循环之前执行了 pBar.Value 一样,或者在每次循环迭代期间都设置了 pBar.Value (我确保没有错误地将 pBar.Value 语句放入for循环中.

The above works fine, however, I would like to be able to reset the progress bar to use it again later. I assumed I could simply set pBar.Value = 1 after the for loop, but strangely this breaks the progress bar. Instead of incrementing steadily like it did previously, the bar does not fill at all. If I set pBar.Value = 50, the bar begins at the halfway point and does not move. It is behaving like pBar.Value is executing before the for loop, or that pBar.Value is getting set during each loop iteration (I've made sure that I did not mistakenly put the pBar.Value statement inside the for loop).

我认为发生这种情况的唯一原因是C#正在异步运行我的for循环,并且立即设置了该值,然后由于某种原因它没有得到更新.我一直在尝试寻找答案,但是到目前为止,我发现的所有方法都是创建后台工作程序的方法(对此我不感兴趣),而且我重置进度条的方法应该正在工作...

The only reason I could think this is happening, is that C# is running my for loop asynchronously and the value is getting set immediately, then for some reason it isn't getting updated. I've tried looking around for answers to this, but so far all I have found are ways to create background workers (I'm not interested in that) and that my method of resetting the progress bar should be working...

为什么会这样?

推荐答案

好的,这是我尝试回答您的问题的方法:

OK, so here is my attempt at answering your question:

当循环后将其重置为1时,我不认为进度条不会更新".我认为进度条的整个动画都是伪造的.

I don't think that the progress bar does not 'update' when you reset its value to 1 after the loop. I think that the whole animation of the progress bar is quite a fake.

我查看了 ProgressBar 的源代码

I took a look at the source code for ProgressBar here. We note that both ProgressBar.Value and ProgressBar.PerformStep utimately call into UpdatePos that in turn sends a PBM_SETPOS message to the underlying native control. Then MSDN tells us that the bar should redraw each time it receives this message.

但是,我怀疑该控件的真实"动画在某种程度上已经虚拟化了.我的猜测是 PBM_SETPOS 消息相对于位置值会立即考虑在内(我们不希望我们的酒吧每次都不知道其值),但是还必须将它们发送到动画排队.

However, I suspect the 'real' animation of the bar is somewhat virtualized by the control. My guess is that the PBM_SETPOS messages are immediately taken into account with respect to the position value (we wouldn't want our bar not to know its value at every time), but they must also be sent to some queue for the animation.

动画可能要花很长时间,并且如果发生某些需要停止或修改动画的事件,则动画将被取消,例如向后退...假设您交替使用值1和51而不是增加当前值,那么您希望使用哪种动画?

The animation can take quite a long time and it would be canceled if something happens that requires the animation to stop or to be modified, going backwards for instance... Suppose you alternate values 1 and 51 instead of incrementing the current value, what sort of animation do you expect?

我通过以下方式替换了代码中的 pBar.PerformStep(); :

I simulated this by replacing pBar.PerformStep(); in your code by:

pBar.Value = j % 2 * 50 + 1;

,当然也没有结尾的 pBar.Value = 1; .

生成的动画不是1到51之间令人讨厌的往返,而是从1到51的干净平滑过渡:)

The resulting animation is not a sickening round-trip between 1 and 51 but a clean and smooth transition from 1 to 51 :)

我认为这证明了我的观点,即动画正在自行选择所需的值,因此,当迅速将100设置为1时,动画引擎决定不渲染任何东西.

I think this proves my point that the animation is picking what values it wants on its own, so that when quickly setting 1 just after 100, the animation engine decides not to render anything.

因此,如果要避免这种影响,最简单的方法是在使用之前将条值重置为1,而不是仅在之后(和顺便说一句,这可能是更好的编程习惯,不要假设进度条值是正确的)使用时为1).

So if you want to avoid this effect, the simplest thing to do is to reset the bar value to 1 before you use it instead of just after (and btw, it may prove better programming practice not to assume the progress bar value is 1 when you use it).

PS:最后要注意的是,在动画有时间完成

PS: as a final note, the same phenomenon was used as a trick to force a high value on a progress bar before the animation has time to complete here.

就您而言,您可以:

private void GoSlowlyTo100() => pBar.Value = 100;
private void GoQuicklyTo100()
{
    pBar.Maximum = 101;
    pBar.Value = 101;
    pBar.Maximum = 100;
    pBar.Value = 100;
}

希望这能回答您的问题!

Hope this answers your question!

这篇关于C#Winforms-ProgressBar无法正确显示和重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆