Updating ProgressBar value from within a class.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a module which performs data processing job and as each row is
processed progress bar value is incremented. Now I have moved this module in
a separate class. How can I increment the value progress bar which resides on
the main form?

Do I have to pass the progress bar to my class, or there’s some other way?
 
There are many ways to do this of course, but I prefer to use
delegates/events. Off the top of my head, here is a brief explanation on
how to do it:


Somewhere in the namespace or within a class, declare a public delegate that
defines what the method signature looks like:
public delegate void ProgressEventHandler(int percentComplete);

Then, in your data module (class), do something like this:
public MyDataClass
{
public event ProgressEventHandler OnProgress;

....

// fire the event at whatever point you want to..say in this method
public void DoSomeWork()
{
// do some work...then fire the event
if (OnProgress != null)
{
OnProgress(myprogresspercent);
}
}
}


Then, in your main form, you define the method to handle the event:

MyDataClass myDataClass = new MyDataClass();
myDataClass.OnProgress += new ProgressEventHandler(MyProgressHandler);
...
private void MyProgressHandler(int percentComplete)
{
// update your progress bar here.
}

Hopefully the above makes sense :) Anyway, I recommend you do a little
research on delegates and events and see what they have to offer. In the
end, they are more flexible than other paradigms.

-sb
 
Back
Top