Create a progressbar display shared class function

  • Thread starter Thread starter Bill Nguyen
  • Start date Start date
B

Bill Nguyen

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
 
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.MyTask(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(donework * 100 / totalwork)
' not using threads, its better to order the events
Application.DoEvents()
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.ProgressBarControl = ProgressBar1

If MyClass instance is only created by Form2, no problem. Create a public
variable on Form2:

Public ProgressBarControl As ProgressBar

So that you can use it when creating Form2 (assuming Form1 creates Form2):

Dim frmForm2 As New Form2
frmForm2.ProgressBarControl = ProgressBar1
frmForm2.Show()

When Form2 creates MyClass and calls MyTask, can you what is on
ProgressBarControl variable.

Dim objMyClass As New MyClass
objMyClass.MyTask(ProgressBarControl)

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
 
I use that method successfully!
I "invented" it because I needed to do it.
Surprisingly, it actually worked!
 
Hi Mario, Just one thing you may want to note, 'MyClass' is actually a
reserved word, and you can't call your class it, unless you parenthesize it:

Public Class [MyClass]
..
..
..
End Class

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Bill Nguyen said:
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?

Do _not_ create a "shared" control. Instead, pass a reference to the
control or the form to the subroutines.
 
Mario;
Thank you very much for your thorough response.
For the sake of clarity, here's the scenario:

1. Form frmProgress contains control ProgressBar1 and a label field
lblInform to diplay information of the calling routine
2. Form frmMain contains a button btnCalcFreight that will call the function
CalcFreight residing in class myCalcucation (in a separate module).

I would like to call frmProgress.ProgressBar1 from frmMain or any other
forms in the application. The calling routine doesn't have to be a class
function. Please tell me the best way to do it.
Thanks again.
Bill
 
Very well. It's like I said, you should pass the progress bar into the
procedure where the slow loop is.
frmMain (btnCalc) -» frmProgress -» CalcFreight

Public Class frmMain
Inherits System.Windows.Forms.Form
' [Windows Form Designer] ...
Private Sub btnCalc_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalc.Click
' Create frmProgress instance and call CalcFreight
Dim MyfrmProgress As New frmProgress
MyfrmProgress.CallCalcFreight
End Sub
End Class

Public Class frmProgress
Inherits System.Windows.Forms.Form
' [Windows Form Designer] ...
Public Sub CallCalcFreight
Me.Show
CalcFreight(ProgressBar1)
End Sub
End Class

Module myCalculation
Public Sub CalcFreight(ByRef oProgressBar As ProgressBar)
' Main loop start
' do what you are supposed too

' inside the loop compute the percentage of work done; ex:
oProgressBar.Value = Math.Round(donework * 100 / totalwork)
' also inside the loop, do events
Application.DoEvents()

' Main loop end
End Sub
End Module
 
Back
Top