ProgressBar and Form loading

  • Thread starter Thread starter Thomas
  • Start date Start date
T

Thomas

Hello,

I don't know if such question was already posted but I didn't find anything
through different posts.

I have the Main MDI window with a StatusBar. Inside my StatusBar there is a
ProgressBar. When I click on the menu in my MDI Parent, a child form is
loaded. I would like to know if there is a way to increment a progressbar
value while child form is loading ? I tried to use background worker but i
had some cross thread access exceptions. any ideas ? Thanks in advance.

Thomas
 
A fairly simple way is to add event to the child form and fire it a few
times to indicate the progress when the child form is loading.

Psuedo code example:

in the child form:

public class ChildForm:Form
{
...
public event System.EventHanler LoadProgressIncrcemented;

//Assume you place all loading logic in Form_Load()
public ChildForm_Load(...)
{
//Fire first event
if (LoadProgressIncremented!=null) LoadProgressIncremented(this,new
EventArgs());

//Do some loading code here

//Fire second event
if (LoadProgressIncremented!=null) LoadProgressIncremented(this,new
EventArgs());

//Do some loading code here

//Fire third event
if (LoadProgressIncremented!=null) LoadProgressIncremented(this,new
EventArgs());

//Do some loading code here

//Fire more event by dividing the loading process into more steps,
when necessary
......
}

In the Main form, when you instantiate a child form, you can subscribe the
event and in the evenet handler procedure you can then set the progress bar:

public class MainForm:Form
{
....
private ShowChildForm()
{
ChidForm frm=new ChildForm();

//Subcribe child form's loading event
frm.LoadProgressIncremented+=new
EventHandler(ChildForm_LoadProgressInccremented)

//Reset progress bar
ProgressBar1.Value=0
ProgressBar1.Max=10; //Assume you are going to fire child form
loading event 10 times.

//Now strat loading the child form
frm.Show();
}

private void ChildForm_LoadProgressIncremented(object sender, EventArgs
e)
(
//Update progress bar
if (ProgressBar1.Value<=10) ProgressBar1.Value++;
)
}

You can derive your own child form loading event argument from
System.EventArgs to pass more detailed loading information to the main form
with event.
 
Hi Thomas,

BackgroundWorker should do fine, just make sure you don't access the
form or any of its controls within your backgroundWorker_DoWork event
handler. If you're still having troubles, please post some of your
relevant code here.

Andrej
 
Back
Top