PC Review


Reply
Thread Tools Rate Thread

Create a progressbar display shared class function

 
 
Bill Nguyen
Guest
Posts: n/a
 
      7th Oct 2003
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


 
Reply With Quote
 
 
 
 
Mario
Guest
Posts: n/a
 
      7th Oct 2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Joe Fallon
Guest
Posts: n/a
 
      7th Oct 2003
I use that method successfully!
I "invented" it because I needed to do it.
Surprisingly, it actually worked!
--
Joe Fallon




"Mario" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Tom Spink
Guest
Posts: n/a
 
      7th Oct 2003
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


"Mario" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      7th Oct 2003
"Bill Nguyen" <(E-Mail Removed)> scripsit:
> 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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
Bill Nguyen
Guest
Posts: n/a
 
      7th Oct 2003
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



"Mario" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> >

>
>



 
Reply With Quote
 
Mario
Guest
Posts: n/a
 
      7th Oct 2003
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


"Bill Nguyen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>
> "Mario" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance of shared function in clas vs. independent class Chandy Microsoft Dot NET 4 26th Aug 2005 07:32 AM
Updating ProgressBar value from within a class. =?Utf-8?B?Sm9iIExvdA==?= Microsoft Dot NET 1 2nd Mar 2005 05:03 AM
How to Create a Function Point for a class member function. Microsoft Microsoft VC .NET 2 13th Aug 2004 09:00 AM
Create Managed Object in Unmanaged class function Kevin Microsoft VC .NET 1 18th Dec 2003 09:01 PM
how do i create a thread with a class function? Chris LaJoie Microsoft VC .NET 2 5th Dec 2003 05:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:33 AM.