Progress Indicator

Allows you to display a progress indicator from within a VSTO add-in.
Message Box interaction - If you want to display any messages to the user you must do this before the progress bar is closed


From a Windows Form Button


gWindowsForm = Me 
Dim handle As New delegate.NoStrings(AddressOf modProgressBar.ProgressBar_Invoke)
Me.Invoke(handler)
Call modProgressBar.ProgressBar_Update("waiting for web service")
-----
-----
gbProgressBar_Running = False


From a Subroutine


Call modProgressBar.ProgressBar_Invoke() 
Call modProgressBar.ProgressBar_Update("waiting for web service")
-----
-----
gbProgressBar_Running = False


Module modProgressBar 

Public gBackgroundWorker_ProgressBar As System.ComponentModel.BackgroundWorker
Public gbProgressBar_Running As Boolean
Public gProgressBar As frmProgressBar
Public gProgressBarListBox As System.Windows.Forms.ListBox
Public gWindowsForm As System.Windows.Forms.Form

Private giCounter As Integer

'**************************************************************************************
Public Sub ProgressBar_Invoke()
Dim objhandle As System.IntPtr
Dim objRect As clsWin32.RECT

Try
objhandle = modWordObjectModel.Word_Handle
gFormNativeWindow_Progress = New System.Windows.Forms.NativeWindow
gFormNativeWindow_Progress.AssignHandle(objhandle)

clsWin32.GetWindowRect(objhandle, objRect)
gProgressBar = New frmProgressBar(CType((objRect.Left + ((objRect.Right - objRect.Left) / 2)), Integer), _
CType((objRect.Top + ((objRect.Bottom - objRect.Top) / 2)), Integer))

gBackgroundWorker_ProgressBar = New System.ComponentModel.BackgroundWorker
gBackgroundWorker_ProgressBar.WorkerSupportsCancellation = True
gBackgroundWorker_ProgressBar.WorkerReportsProgress = True

AddHandler gBackgroundWorker_ProgressBar.DoWork, AddressOf modProgressBar.ProgressBar_DoWork
AddHandler gBackgroundWorker_ProgressBar.ProgressChanged, AddressOf modProgressBar.ProgressBar_UpdateMessage
AddHandler gBackgroundWorker_ProgressBar.RunWorkerCompleted, AddressOf modProgressBar.ProgressBar_Completed

gbProgressBar_Running = True

giCounter = 0
gBackgroundWorker_ProgressBar.RunWorkerAsync()

gProgressBar.Show(gFormNativeWindow_Progress)

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
End Try
End Sub
'**************************************************************************************
Public Sub ProgressBar_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs)
Try

Do While (gbProgressBar_Running = True)
System.Windows.Forms.Application.DoEvents()
Loop

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
End Try
End Sub
'**************************************************************************************
Public Sub ProgressBar_Update(ByVal sMessage As String)

Try
gBackgroundWorker_ProgressBar.ReportProgress(giCounter, sMessage)
giCounter = giCounter + 1
gWindowsForm.Refresh()
System.Windows.Forms.Application.DoEvents()

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
End Try
End Sub
'**************************************************************************************
Public Sub ProgressBar_UpdateMessage(ByVal sender As Object, _
ByVal e As System.ComponentModel.ProgressChangedEventArgs)
Try

Call modGeneral.Error_ListBoxUpdateText(gProgressBarListBox, giCounter, e.UserState.ToString)
gProgressBarListBox.Refresh()
gProgressBarListBox.Parent.Refresh()
gProgressBar.Refresh()

gWindowsForm.Refresh()
System.Windows.Forms.Application.DoEvents()

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
End Try
End Sub
'**************************************************************************************
Public Sub ProgressBar_Completed(ByVal sender As Object, _
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)

Try
gProgressBar.Close()

If Not (gBackgroundWorker_ProgressBar Is Nothing) Then
If (gBackgroundWorker_ProgressBar.IsBusy = True) Then
gBackgroundWorker_ProgressBar.CancelAsync()
End If
End If

Catch ex As Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
End Try
End Sub
'**************************************************************************************


© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext