Hi Bill,
You can call the Value property from other "routines" (methods), as long as
the progress bar object is accessible to those methods. If you have a form
with a progress bar, that object is accessible to all methods in the same
form class.
However, to change Value property from other classes, you need to use a
different strategy.
You have several choices, some simpler, some more complex (callbacks,
threads... nasty stuff!).
I'm gona show you a simple way:
Imagine you have a class called MyClass with a method called MyTask where a
long job will be done, and you want to track progress with a ProgressBar1
control on Form1. When you call MyTask sub, you can pass as argument the
ProgressBar1 control. Example:
Dim objMyClass As New MyClass
objMyClass.MyTa sk(ProgressBar1 )
This way, your class method needs to be changed to receive ByRef the
progress bar control. Note the ByRef (not ByVal), so that the object can be
changed.
Public Class MyClass
Public Sub MyTask(ByRef objProgBar As ProgressBar)
While donework < totalwork
' do main job
' show progress
objProBar.Value = Math.Round(done work * 100 / totalwork)
' not using threads, its better to order the events
Application.DoE vents()
End While
End Sub
End Class
That's it. Lets just consider a scenario where you dont call the MyTask
function from the object where the progress bar is. For instance if you have
two forms, progress bar is on Form1, and the call to MyTask is on a button
click event on Form2. No problem. If MyClass instance is already created
when Form1 runs, or it is created by Form1, you can just create a variable
on MyClass to receive the control, and pass it at Form1:
Dim objMyClass As New MyClass
objMyClass.Prog ressBarControl = ProgressBar1
If MyClass instance is only created by Form2, no problem. Create a public
variable on Form2:
Public ProgressBarCont rol As ProgressBar
So that you can use it when creating Form2 (assuming Form1 creates Form2):
Dim frmForm2 As New Form2
frmForm2.Progre ssBarControl = ProgressBar1
frmForm2.Show()
When Form2 creates MyClass and calls MyTask, can you what is on
ProgressBarCont rol variable.
Dim objMyClass As New MyClass
objMyClass.MyTa sk(ProgressBarC ontrol)
As I said there are other ways to handle your challenge. Advanced code,
taking full advantage of Framework, probably would thread the MyTask Sub, or
even thread pool what is supposed to be done there. That is a little bit
hardcore, and I think you should try this first.
Best regards,
Mario
"Bill Nguyen" <bi***@jaco.com > wrote in message
news:eN******** *****@TK2MSFTNG P11.phx.gbl...
How can I create a shared function to display process progress that can be
called from other routines within an application? Any example that I can
follow?
Thanks
Bill