Progress form with class to manipulate

B

Bill Schanks

I have a vs2005 Windows Form Project that has a form with 3 Progress
Meters on it. I would like to have a class that handles all updates to
this form. There will be multiple subs/functions that need to update
this form. Here is the class that I have now:

But it doesn't work. When first called it does. But the second call to
it does not work. I am getting a System.NullReferenceException.

Public Class clsProgress

Private m_frmProgress As Progress
Private Const m_sPROGRESS_FORM_NAME As String = "Progress"

Public Sub LoadProgress()
Dim f As Form
Dim bLoaded As Boolean

For Each f In My.Application.OpenForms
If UCase(f.Name) = UCase(m_sPROGRESS_FORM_NAME) Then
bLoaded = True
Exit For
End If
Next

If Not bLoaded Then
m_frmProgress = New Progress
m_frmProgress.Show()
End If
End Sub

Public Sub CloseProgress()
m_frmProgress.Close()
End Sub

Public Sub InitDownloadProgress(ByVal lMax As Long)
m_frmProgress.progDownload.Value = 0
m_frmProgress.progDownload.Maximum = lMax
Application.DoEvents()
End Sub

Public Sub IncrementDownloadProgress(ByVal lProgress As Long,
ByVal sUpdateText As String)
m_frmProgress.lblDownload.Text = sUpdateText
With m_frmProgress.progDownload
If .Value < .Maximum Then .Value = lProgress
Application.DoEvents()
End With
End Sub

Public Sub InitFileProgress1(ByVal lMax As Long)
If Not m_frmProgress.progFile1.Visible Then
m_frmProgress.lblFile1.Visible = True
m_frmProgress.progFile1.Visible = True
End If

m_frmProgress.progFile1.Value = 0
m_frmProgress.progFile1.Maximum = lMax
Application.DoEvents()
End Sub

Public Sub IncrementFileProgress1(ByVal lProgress As Long, ByVal
sUpdateText As String)
m_frmProgress.lblFile1.Text = sUpdateText
With m_frmProgress.progFile1
If .Value < .Maximum Then .Value = lProgress
Application.DoEvents()
End With
End Sub

Public Sub InitFileProgress2(ByVal lMax As Long)

If Not m_frmProgress.progFile2.Visible Then
m_frmProgress.lblFile2.Visible = True
m_frmProgress.progFile2.Visible = True
End If

m_frmProgress.progFile2.Value = 0
m_frmProgress.progFile2.Maximum = lMax
Application.DoEvents()
End Sub

Public Sub IncrementFileProgress2(ByVal lProgress As Long, ByVal
sUpdateText As String)
m_frmProgress.lblFile1.Text = sUpdateText
With m_frmProgress.progFile2
If .Value < .Maximum Then .Value = lProgress
Application.DoEvents()
End With
End Sub

End Class
 
J

Jack Jackson

You forgot to tell us on what line you get the exception.

I noticed that in LoadProgress() you don't set m_frmProgress if the
form is already loaded. That would certainly cause a NullReference
exception in the other routines.
 
B

Bill Schanks

It was throwing the exception when it tried to update the progress
meter in Sub 'IncrementFileProgress1'.
But based on your comment I changed Sub 'LoadProgess' as follows:

So, I can run the project now without an error however the progress
meter doesn't update. The second progress meter that is.

Public Sub LoadProgress()
Dim f As Form
Dim bLoaded As Boolean

For Each f In My.Application.OpenForms
If UCase(f.Name) = UCase(m_sPROGRESS_FORM_NAME) Then
bLoaded = True
Exit For
End If
Next

If Not bLoaded Then
m_frmProgress = New Progress
m_frmProgress.Show()
Application.DoEvents()
Else
m_frmProgress = Progress
End If
End Sub
 
J

Jack Jackson

I don't understand your change. In this line:
m_frmProgress = Progress
what is "Progress"? The only thing I see named Progress is the name
of a class. You need to set m_frmProgress to a reference to the
Progress form that you found.

I would have done it this way:
For Each f In My.Application.OpenForms
If UCase(f.Name) = UCase(m_sPROGRESS_FORM_NAME) Then
bLoaded = True m_frmProgress = f
Exit For
End If
Next

Also, IncrementFileProgress2 changes m_frmProgress.lblFile1, not
lblFile2.
 
B

Bill Schanks

Progress is the name of the form. Now if I change it to m_frmProgress
= f then when I call one of the initprogress Subs (e.g.
InitDownloadProgress) it throws an exception on the first line of that
sub.

Also to assign m_frmProgress = f I had to change my declaration at the
top to 'Private m_frmProgress' from Private m_frmProgress As Progress

I get what you are saying about assigning m_frmProgress to the found
form. I tried using f.name, f.ActiveForm but none of it works.
 
J

Jack Jackson

I don't understand what is going on. If you are not running with
OPTION STRICT ON, you should. Not using that can hide a lot of bugs.

I still don't understand the "m_frmProgress = Progress". That makes
no sense to me. Progress is the name of a class, not a reference to a
form.

To fix the casting problem with setting m_frmProgress to f:

For Each f In My.Application.OpenForms
Dim p As Progress = TryCast(f, Progress)
If p IsNot Nothing Then
bLoaded = True
m_frmProgress = p
Exit For
End If
Next
 
B

Bill Schanks

That worked.. Thank you for your persistence.

I don't understand what is going on. If you are not running with
OPTION STRICT ON, you should. Not using that can hide a lot of bugs.

I still don't understand the "m_frmProgress = Progress". That makes
no sense to me. Progress is the name of a class, not a reference to a
form.

To fix the casting problem with setting m_frmProgress to f:

For Each f In My.Application.OpenForms
Dim p As Progress = TryCast(f, Progress)
If p IsNot Nothing Then
bLoaded = True
m_frmProgress = p
Exit For
End If
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top