Skip to main content
deleted 8 characters in body
Source Link
RubberDuck
  • 31.2k
  • 6
  • 74
  • 177
Option Explicit

' ***************************************************************
' Class: Form_ProgressBar 
' Popup progress bar 
' 
' Author: Christopher J. McClellan 
'     http://christopherjmcclellan.wordpress.com/
' Significant input from Mat's Mug 
'     http://codereview.stackexchange.com/users/23788/mats-mug
'
' Published under Creative Commons Attribution-Share Alike 
' http://creativecommons.org/licenses/by-sa/3.0/ 
'
' You are free to change, distribute, and pretty much do 
'  whatever you like with the code, but you must give credit 
'  to the original author and publish any derivative of this 
'  code under the same license. 
' ***************************************************************

Public Enum ProgressBarDisplayMode
    PBarDisplayPercent = 0
    PBarDisplayExecuting = 1
End Enum

Private Type TProgressBarMembers
    Mode As ProgressBarDisplayMode
    CurrentProgress As Long
    Steps As Long
End Type

Private Const maxBoxWidth As Long = 7200
Private Const executingMessage As String = "Executing..."

Private this As TProgressBarMembers

Public Property Get PercentComplete() As Double
'read only
    PercentComplete = this.CurrentProgress / this.Steps * 100
End Property

Public Property Let Mode(newValue As ProgressBarDisplayMode)
    this.Mode = newValue
End Property

Public Property Get Mode() As ProgressBarDisplayMode
    Mode = this.Mode
End Property

Public Property Let CurrentProgress(newValue As Long)
    this.CurrentProgress = newValue
    ' keep the graphics in sync
    RepaintMe
End Property

Public Property Get CurrentProgress() As Long
    CurrentProgress = this.CurrentProgress
End Property

Property Let Steps(newValue As Long)
    this.Steps = newValue
End Property

Public Sub Init(Steps As Long, Mode As ProgressBarDisplayMode, Optional Caption As String = "Loading...")
    Me.Mode = Mode
    Me.Caption = Caption
    this.CurrentProgress = 0
    this.Steps = Steps
    Me.boxProgress.Width = 0
    
    Select Case Mode
        Case PBarDisplayExecuting
            Me.txtStatus = executingMessage
        Case Else
            Me.txtStatus = "Ready"
    End Select
    
    Me.Visible = True
End Sub

Private Sub RepaintMe()
    
    If Not this.Mode = PBarDisplayExecuting Then
        UpdateProgressMessage
    End If
    
    UpdateBoxWidth
    
    Me.Repaint
    DoEvents
End Sub

Private Sub UpdateProgressMessage()
    Select Case this.Mode
        Case PBarDisplayPercent
            ' format #0 makes a 1 or 2 digit number without decimals
            Me.txtStatus = Format(Me.PercentComplete, "#0") & " % Complete"
        Case PBarDisplayExecuting
            Me.txtStatus = executingMessage
    End Select
End Sub

Private Sub UpdateBoxWidth()
    Me.boxProgress.Width = (this.CurrentProgress / this.Steps) * maxBoxWidth
End Sub
Option Explicit

' ***************************************************************
' Class: Form_ProgressBar 
' Popup progress bar 
' 
' Author: Christopher J. McClellan 
'     http://christopherjmcclellan.wordpress.com/
' Significant input from Mat's Mug 
'     http://codereview.stackexchange.com/users/23788/mats-mug
'
' Published under Creative Commons Attribution-Share Alike 
' http://creativecommons.org/licenses/by-sa/3.0/ 
'
' You are free to change, distribute, and pretty much do 
'  whatever you like with the code, but you must give credit 
'  to the original author and publish any derivative of this 
'  code under the same license. 
' ***************************************************************

Public Enum ProgressBarDisplayMode
    PBarDisplayPercent = 0
    PBarDisplayExecuting = 1
End Enum

Private Type TProgressBarMembers
    Mode As ProgressBarDisplayMode
    CurrentProgress As Long
    Steps As Long
End Type

Private Const maxBoxWidth As Long = 7200
Private Const executingMessage As String = "Executing..."

Private this As TProgressBarMembers

Public Property Get PercentComplete() As Double
'read only
    PercentComplete = this.CurrentProgress / this.Steps * 100
End Property

Public Property Let Mode(newValue As ProgressBarDisplayMode)
    this.Mode = newValue
End Property

Public Property Get Mode() As ProgressBarDisplayMode
    Mode = this.Mode
End Property

Public Property Let CurrentProgress(newValue As Long)
    this.CurrentProgress = newValue
    ' keep the graphics in sync
    RepaintMe
End Property

Public Property Get CurrentProgress() As Long
    CurrentProgress = this.CurrentProgress
End Property

Property Let Steps(newValue As Long)
    this.Steps = newValue
End Property

Public Sub Init(Steps As Long, Mode As ProgressBarDisplayMode, Optional Caption As String = "Loading...")
    Me.Mode = Mode
    Me.Caption = Caption
    this.CurrentProgress = 0
    this.Steps = Steps
    Me.boxProgress.Width = 0
    
    Select Case Mode
        Case PBarDisplayExecuting
            Me.txtStatus = executingMessage
        Case Else
            Me.txtStatus = "Ready"
    End Select
    
    Me.Visible = True
End Sub

Private Sub RepaintMe()
    
    If Not this.Mode = PBarDisplayExecuting Then
        UpdateProgressMessage
    End If
    
    UpdateBoxWidth
    
    Me.Repaint
    DoEvents
End Sub

Private Sub UpdateProgressMessage()
    Select Case this.Mode
        Case PBarDisplayPercent
            ' format #0 makes a 1 or 2 digit number without decimals
            Me.txtStatus = Format(Me.PercentComplete, "#0") & " % Complete"
        Case PBarDisplayExecuting
            Me.txtStatus = executingMessage
    End Select
End Sub

Private Sub UpdateBoxWidth()
    Me.boxProgress.Width = (this.CurrentProgress / this.Steps) * maxBoxWidth
End Sub
Option Explicit

' ***************************************************************
' Class: Form_ProgressBar 
' Popup progress bar 
' 
' Author: Christopher J. McClellan 
'     http://christopherjmcclellan.wordpress.com/
' Significant input from Mat's Mug 
'     http://codereview.stackexchange.com/users/23788/mats-mug
'
' Published under Creative Commons Attribution-Share Alike 
' http://creativecommons.org/licenses/by-sa/3.0/ 
'
' You are free to change, distribute, and pretty much do 
'  whatever you like with the code, but you must give credit 
'  to the original author and publish any derivative of this 
'  code under the same license. 
' ***************************************************************

Public Enum ProgressBarDisplayMode
    PBarDisplayPercent
    PBarDisplayExecuting
End Enum

Private Type TProgressBarMembers
    Mode As ProgressBarDisplayMode
    CurrentProgress As Long
    Steps As Long
End Type

Private Const maxBoxWidth As Long = 7200
Private Const executingMessage As String = "Executing..."

Private this As TProgressBarMembers

Public Property Get PercentComplete() As Double
'read only
    PercentComplete = this.CurrentProgress / this.Steps * 100
End Property

Public Property Let Mode(newValue As ProgressBarDisplayMode)
    this.Mode = newValue
End Property

Public Property Get Mode() As ProgressBarDisplayMode
    Mode = this.Mode
End Property

Public Property Let CurrentProgress(newValue As Long)
    this.CurrentProgress = newValue
    ' keep the graphics in sync
    RepaintMe
End Property

Public Property Get CurrentProgress() As Long
    CurrentProgress = this.CurrentProgress
End Property

Property Let Steps(newValue As Long)
    this.Steps = newValue
End Property

Public Sub Init(Steps As Long, Mode As ProgressBarDisplayMode, Optional Caption As String = "Loading...")
    Me.Mode = Mode
    Me.Caption = Caption
    this.CurrentProgress = 0
    this.Steps = Steps
    Me.boxProgress.Width = 0
    
    Select Case Mode
        Case PBarDisplayExecuting
            Me.txtStatus = executingMessage
        Case Else
            Me.txtStatus = "Ready"
    End Select
    
    Me.Visible = True
End Sub

Private Sub RepaintMe()
    
    If Not this.Mode = PBarDisplayExecuting Then
        UpdateProgressMessage
    End If
    
    UpdateBoxWidth
    
    Me.Repaint
    DoEvents
End Sub

Private Sub UpdateProgressMessage()
    Select Case this.Mode
        Case PBarDisplayPercent
            ' format #0 makes a 1 or 2 digit number without decimals
            Me.txtStatus = Format(Me.PercentComplete, "#0") & " % Complete"
        Case PBarDisplayExecuting
            Me.txtStatus = executingMessage
    End Select
End Sub

Private Sub UpdateBoxWidth()
    Me.boxProgress.Width = (this.CurrentProgress / this.Steps) * maxBoxWidth
End Sub
Source Link
RubberDuck
  • 31.2k
  • 6
  • 74
  • 177

Adding the final version of the code here for anyone interested.

Option Explicit

' ***************************************************************
' Class: Form_ProgressBar 
' Popup progress bar 
' 
' Author: Christopher J. McClellan 
'     http://christopherjmcclellan.wordpress.com/
' Significant input from Mat's Mug 
'     http://codereview.stackexchange.com/users/23788/mats-mug
'
' Published under Creative Commons Attribution-Share Alike 
' http://creativecommons.org/licenses/by-sa/3.0/ 
'
' You are free to change, distribute, and pretty much do 
'  whatever you like with the code, but you must give credit 
'  to the original author and publish any derivative of this 
'  code under the same license. 
' ***************************************************************

Public Enum ProgressBarDisplayMode
    PBarDisplayPercent = 0
    PBarDisplayExecuting = 1
End Enum

Private Type TProgressBarMembers
    Mode As ProgressBarDisplayMode
    CurrentProgress As Long
    Steps As Long
End Type

Private Const maxBoxWidth As Long = 7200
Private Const executingMessage As String = "Executing..."

Private this As TProgressBarMembers

Public Property Get PercentComplete() As Double
'read only
    PercentComplete = this.CurrentProgress / this.Steps * 100
End Property

Public Property Let Mode(newValue As ProgressBarDisplayMode)
    this.Mode = newValue
End Property

Public Property Get Mode() As ProgressBarDisplayMode
    Mode = this.Mode
End Property

Public Property Let CurrentProgress(newValue As Long)
    this.CurrentProgress = newValue
    ' keep the graphics in sync
    RepaintMe
End Property

Public Property Get CurrentProgress() As Long
    CurrentProgress = this.CurrentProgress
End Property

Property Let Steps(newValue As Long)
    this.Steps = newValue
End Property

Public Sub Init(Steps As Long, Mode As ProgressBarDisplayMode, Optional Caption As String = "Loading...")
    Me.Mode = Mode
    Me.Caption = Caption
    this.CurrentProgress = 0
    this.Steps = Steps
    Me.boxProgress.Width = 0
    
    Select Case Mode
        Case PBarDisplayExecuting
            Me.txtStatus = executingMessage
        Case Else
            Me.txtStatus = "Ready"
    End Select
    
    Me.Visible = True
End Sub

Private Sub RepaintMe()
    
    If Not this.Mode = PBarDisplayExecuting Then
        UpdateProgressMessage
    End If
    
    UpdateBoxWidth
    
    Me.Repaint
    DoEvents
End Sub

Private Sub UpdateProgressMessage()
    Select Case this.Mode
        Case PBarDisplayPercent
            ' format #0 makes a 1 or 2 digit number without decimals
            Me.txtStatus = Format(Me.PercentComplete, "#0") & " % Complete"
        Case PBarDisplayExecuting
            Me.txtStatus = executingMessage
    End Select
End Sub

Private Sub UpdateBoxWidth()
    Me.boxProgress.Width = (this.CurrentProgress / this.Steps) * maxBoxWidth
End Sub
Post Made Community Wiki by RubberDuck