问题描述
我正在尝试在用户窗体中创建一个进度条,而不是使用单独的进度条,因为就其顶部或背景而言,这似乎并不可靠.因此,进度栏运行良好,但是对于进度栏进行的每次更新,它都会重新绘制整个用户窗体.是否可以只刷新进度条而不是整个用户表单?
I am trying make a progress bar inside a userform, instead of having a seperate progressbar, because this seems to be unreliable in the respect if it will be on top or in the background. So the progressbar is working fine, however it makes the whole userform repaint for every update the progressbar does. Is it possible to just refresh the progressbar instead of the whole userform?
我当前的代码看起来像这样:
My current code do look like this:
Public Sub progress(pctCompl As Single)
Me.Text.caption = Format(pctCompl, "##") & "% Completed"
Me.Bar.width = Round(pctCompl * 10, 5)
If Me.Bar.width Mod 20 = 0# Then
Me.Repaint
End If
DoEvents
End Sub
推荐答案
在对象浏览器中,当在MSForm
库中搜索Repaint
时,将找到UserForm
,但也会找到Frame
.因此,如果要直接使用用户窗体上的进度,则可以尝试将Bar
包装到MSForms.Frame
中,并且在需要重新绘制时,只需在此框架中调用它即可.在这种情况下,其余用户窗体及其控件不应受到影响,而仅应重新绘制框架.
In object browser when searching for Repaint
within MSForm
library the UserForm
will be found but Frame
as well. So if you want to use the progress on the user form directly, then you can try to wrap your Bar
into MSForms.Frame
and when repaint is required, then just call it for this frame. In this case the remaining user form with its controls should not be influenced of this and only the frame should be repainted.
Me.Frame1.Repaint ' Me is the main user form
这可以通过单独的 modal 表格轻松解决,该表格将显示进度.此表单将具有取消按钮,并在显示时引发事件Start
,以便调用表单可以开始执行长时间运行的工作,而当用户单击取消按钮以过早地取消进餐时,事件将开始事件Cancel
.例如,HTH.
This could be solved easily with separate modal form which will show the progress. This form will have cancel button and will raise event Start
when it is displayed so the calling form can start doing the long running job and event Cancel
when user clicked cancel button for cancelling of the precess prematurely. Example, HTH.
Option Explicit
Public Event Start()
Public Event Cancel(ByRef ignore As Boolean)
Private Sub CommandButtonCancel_Click()
Dim ignoreCance As Boolean
ignoreCance = False
RaiseEvent Cancel(ignoreCance)
If ignoreCance Then
Exit Sub
Else
Unload Me
End If
End Sub
Private Sub UserForm_Activate()
Static isActivated As Boolean
Me.Repaint
If Not isActivated Then
' ensure only once activation
isActivated = True
RaiseEvent Start
Unload Me
End If
End Sub
Public Sub Update(ByVal newValue As Long)
' update progrress bar here
' Pseudo code: Me.Progress.Value = newValue etc.
Me.Repaint
End Sub
Option Explicit
Private WithEvents progress As UserFormProgress
Private cancelProgresForm As Boolean
Private Sub CommandButtonDoSomethingLongRunning_Click()
Set progress = New UserFormProgress
' Set progress form
' progress.Caption = ...
' progress.MaxValue = 123456789
progress.Show vbModal
End Sub
Private Sub progress_Start()
' Callback from progress form, here runs the lung running process
' calculate some complete status value
Dim completeValue As Long
completeValue = 0
cancelProgresForm = False
Do
completeValue = completeValue + 1
progress.Update completeValue
DoEvents
Loop While cancelProgresForm = False And completeValue < progres.MaxValue
End Sub
Private Sub progress_Cancel(ignore As Boolean)
If MsgBox("Do you want to cancel?", vbQuestion Or vbYesNo) = vbNo Then
ignore = True
Else
ignore = False
cancelProgresForm = True
End If
End Sub
这篇关于用户窗体中的进度栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!