By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,685 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,685 IT Pros & Developers. It's quick & easy.

Create a progressbar display shared class function

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
"Bill Nguyen" <bi***@jaco.com> wrote in message
news:eN*************@TK2MSFTNGP11.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

Nov 20 '05 #2

P: n/a
I use that method successfully!
I "invented" it because I needed to do it.
Surprisingly, it actually worked!
--
Joe Fallon


"Mario" <mz******@DONTWANTSPAMmail.pt> wrote in message
news:Ov**************@TK2MSFTNGP11.phx.gbl...
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
"Bill Nguyen" <bi***@jaco.com> wrote in message
news:eN*************@TK2MSFTNGP11.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


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.