473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Create a progressbar display shared class function

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
2 3169
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Teeples | last post by:
Is there a way for code from one class of C# to send a communication to a progressbar in another class to update the bar during runtime? -- Steve
8
by: needin4mation | last post by:
Please consider: foreach (ListViewItem item in listViewFiles.Items) { // Display the ProgressBar control. pBar1.Visible = true; // Set Minimum to 1 to represent the first file being copied....
0
by: tlemcenvisit | last post by:
Refreshing a progressBar Hello I have a function which takes much execution time. I’d like to display its progress in a progressBar. For the moment I make like that: for(y=0;y<height;y++)...
1
by: AnAnimal | last post by:
How would I display the Windows FILECOPY.AVI above a ProgressBar control?
4
by: pmcguire | last post by:
Rather than putting a progress bar on all of my forms to show progress during time consuming tasks, I made a form called frmProgress with 2 controls, a Label and a ProgressBar. Suppose I expose 1...
4
by: Joe HM | last post by:
Hello - I have a Base Class where I want a New() implemented that can be called from the outside. This New() should create an instance of the appropriate cDerivedX Class ... The following...
24
by: M O J O | last post by:
Hi, Instead of doing this.... Public Class Form1 Public Shared Sub CreateAndShow() Dim f As New Form1 f.Show() End Sub
10
by: tshad | last post by:
I have a Dll I created in VS 2000. The namespace is MyFunctions and the Class is CryptoUtil. I have a program that is using the Class but it can't access it directly. I have a class (below)...
11
by: Mark B | last post by:
I want to display a pre-designed graphical 'performance badge' on certain webpages (round, about 2cm diameter) next to a salesperson's details. I have a function,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.