How to increment form1.progressbar from module1

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I have a module that is called from Form1 that performs a long process.

I have a Progressbar on Form1 that I need to increment to show the user
that stuff is happening.

I added this code to Form1:

Public Sub IncProgBar()
ProgressBar1.Increment(1)
ProgressBar1.Update()
End Sub

However the sub is not visible from Module1.

How can I increment the progressbar from the module?
 
Terry,

Terry Olsen said:
I have a module that is called from Form1 that performs a long process.

I have a Progressbar on Form1 that I need to increment to show the user
that stuff is happening.

I added this code to Form1:

Public Sub IncProgBar()
ProgressBar1.Increment(1)
ProgressBar1.Update()
End Sub

However the sub is not visible from Module1.

You will have to make a reference to either the form or the control avilable
to the method defined in the module. This can be accomplished by adding a
parameter to the method which takes a reference to the form or control.
However, I prefer a solution which decouples the form and the worker class
and makes the worker class reusable. To do so, define an event in the
worker class which gets raised when progress changes. The client form which
uses the class can add a handler to the progress change event and update the
user interface accordingly.

\\\
Public Class FooWorker
Public Event ProgressChanged( _
ByVal sender As Object, _
ByVal e As ProgressChangedEventArgs _
)

Public Sub DoWork()

' Perform operation here.
For i As Integer = 1 To 100
RaiseEvent _
ProgressChanged( _
Me, _
New ProgressChangedEventArgs(i)
)
...
Next i
End Sub
End Class

Public Class ProgressChangedEventArgs
Inherits EventArgs

Private m_Progress As Single

Public Sub New(ByVal Progress As Single)
Me.Progress = Progress
End Sub

Public Property Progress() As Single
Get
Return m_Progress
End Get
Set(ByVal Value As Single)
m_Progress = Value
End Set
End Property
End Class
///

Client class using the worker object:

\\\
Private WithEvents m_Worker As New Worker()

Private Sub m_Worker_ProgressChanged( _
ByVal sender As Object, _
ByVal e As ProgressChangedEventArgs _
)
Me.LabelProgress.Text = CStr(e.Progress)
End Sub

Private Sub Button1_Click(...)
m_Worker.DoWork()
End Sub
///

The sample above doesn't implement the full event pattern used in the .NET
Framework. I suggest to read the chapters about events in the documentation
in order to get a broader view on the topic.
 

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

Back
Top