blah blah blah is here! blah blah » Close

up0down
link

Hi,
I'm facing a problem. I have 3 forms (Wizard)
After i close form 2 and open form 3, i'm trying to update a progress bar, but nothing happens.
This is the declaration:
/Wizard.ThridStep thrd = new AnaPat.Wizard.ThridStep();
thrd.progressBar1.value = value;
i all so try like this but no luck!
Wizard.ThridStep thrd = (Wizard.ThridStep)System.Windows.Forms.Application.OpenForms["Wizard.ThridStep"];
System.Windows.Forms.Label label2 = (System.Windows.Forms.Label)thrd.Controls["label2"];
ProgressBar progressBar1 = (ProgressBar)thrd.Controls["progressBar1"];
progressBar1.Value = 10;
the error is: Object reference not set to an instance of an object.
Any ideas? David

last answered 2 years ago

1 answers

up0down
link

Hi,
this means that some of the name s is wrong check the values of the property Name of these this controls and see what is the difference
Best Regards,
Iordan

up0down
link

There's also another possibility - that the names are correct but the label and/or progressbar has not been placed directly on the form but is inside some container such as a panel or groupbox.
If that's the case, then you'll need to drill down into the container's Controls collection to find it.

up0down
link

Yes vulpes you ar right the progress bar is inside of a group box!
how can i access the item inside of group box?
thanks!
david

up0down
link

If it's inside groupBox1 say, then you can get to it as follows:
GroupBox groupBox1 = (GroupBox)thrd.Controls["groupBox1"];
ProgressBar progressBar1 = (ProgressBar)groupBox1.Controls["progressBar1"];
progressBar1.Value = 10;

up0down
link

Yes, vulpes it work's!
But now i get this error
Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.
any idea?
thanks

up0down
link

Hi,
This means that you are accessing the control from background thread.
There are few ways to avoid this if you are more spesific about your
situation i can tell you what is the best way to do it. One way is to use Control.Invoke();
Best Regards,
Iordan

up0down
link

Ok
I have a background worker that export same data to excel
i have a foreach that export row by row, inside of this i want to update the progress bar.
here is the code:
foreach (DataRow dr in dt.Rows)
{
for (int k = 0; k < cols; k++)
{
string cell = alfabet[k] + cellRow;
aRange = ws.get_Range(cell, cell);
string temp = dr[k].ToString();
cellCol++;
aRange.Value2 = temp;
}
progreessBar1.value = cellRow;

cellRow++;
cellCol = 1;
}
I have a background worker that call's the function and inside of the function is this foreach!
thanks

up0down
link

Hi, try this
//declare with the other variable int the from
delegate void MyDelegate(int a );
....
// in the foreach cycle put this
BeginInvoke(
new MyDelegate(MyMethod1),
new object[]{cellRow} );
//Declaration of method
void MyMethod2(int a )
{
progreessBar1.value = a ;
}
Best Regards,
iordan

up0down
link

it tells me:
BeginInvoke does not exist in the current context!
Thanks

up0down
link

It sounds like the code in question is not within a form but within a different class.
If the form reference 'thrd' is still in scope, I'd see if this monstrosity (albeit self-contained) will work:
thrd.BeginInvoke(new MethodInvoker(delegate(){progressBar1.Value = cellRow;}));

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback