简体   繁体   English

如何更新作为进度条的用户表单?

[英]How to update a userform that is a progress bar?

I'm trying to update my userform Updating (which is essentially a progress bar).我正在尝试更新我的用户表单Updating (本质上是一个进度条)。

It doesn't update the first time the userform is called and the second time it only updates the label description and not the width of the bar.它不会在第一次调用用户表单时更新,第二次它只会更新标签描述而不是栏的宽度。

Sub UpdateUpdatingUF(filenum As Integer, filecount As Integer)
    Dim filenumdbl As Double
    Dim filecountdbl As Double
    Dim boxwidth As Integer
    Dim barwidth As Integer
    Dim boxwidthdbl As Double
    filenumdbl = CDbl(filenum)
    filecountdbl = CDbl(filecount)
    boxwidthdbl = CDbl(boxwidth)
    boxwidth = 300
    barwidth = CInt(boxwidthdbl * filenumdbl / filecountdbl)
    With Updating
        .Label3.Caption = "Running file: " & CStr(filenum) & " / " & CStr(filecount)
        .ProgressBar.Width = barwidth
    End With
End Sub

I'm probably declaring too many variables but I am trying to make sure that that isn't the cause.我可能声明了太多变量,但我试图确保这不是原因。

I'm testing with the procedure below.我正在使用以下程序进行测试。

Sub TestUpdate()
    Updating.Show
    Call UpdateUpdatingUF(3, 7)
    DoEvents
    Updating.Repaint
End Sub

Replace代替

Updating.Show

with

Updating.Show vbModeless

Too late but:太晚了,但是:

boxwidthdbl = CDbl(boxwidth) boxwidth = 300

those two lines should be in the reverse order :这两行应该是相反的顺序:

boxwidth = 300
boxwidthdbl = CDbl(boxwidth)

as you are using a variable that has not been initialised.因为您正在使用尚未初始化的变量。

The proposed solution is correct if you want to update during another operation (inside a do/while or a for/next loop for instance).如果您想在另一个操作期间更新(例如在 do/while 或 for/next 循环中),建议的解决方案是正确的。 vbModeless instructs the program not to wait for the form to be closed. vbModeless指示程序不要等待窗体关闭。 Updating while the form is displayed is made thanks to that and the DoEvents instruction.多亏了这一点和DoEvents指令,才能在显示表单时进行更新。

If the update is only required once, you should also reverse the updating of the values and the display of the form:如果只需要更新一次,您还应该反转值的更新和表单的显示:

Call UpdateUpdatingUF(3, 7)
Updating.Show

--> this is why you had to test twice to see the results (you displayed the form before updating the values and had to close it to update the values). --> 这就是为什么你必须测试两次才能看到结果(你在更新值之前显示了表单并且必须关闭它来更新值)。

Like this, no more need for doevents and repaint.像这样,不再需要 doevents 和重绘。

To be sure to unload the form, if needed for tests purpose, use Unload Updating .为了确保卸载表单,如果需要用于测试目的,请使用Unload Updating

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

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