I have a small problem. I have a class that does stuff. And I have my form. Now when I press a button. My class does something. Now what I want is that I my progress bar will update. But my class cant see the progress bar. I know why but how would I fix this ???
11 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
It's probably because the two Panels are an integral part of the SplitContainer control rather than independent controls added to its Controls collection. Also they're of type SplitterPanel rather than Panel.
Yes, it'll work for any control on any open form in the current application.
Sometimes, though, you need to go through more than two steps. For example if you wanted to add text to a textbox on a panel within a form, then you'd need to get references to the form, panel and textbox before using the latter's Text property to add the text.
This is because the textbox would be in the panel's Controls collection rather than the form's.
danie van ophemPosted Oct 8, 2008, 3:10 PM
AlanPosted Oct 8, 2008, 2:16 PM
I don't know whether you'll get a lot more from them but these are the relevant MSDN links:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.splitcontainer.aspx
danie van ophemPosted Oct 8, 2008, 1:56 PM
aha thank you. do you know wher i can find some more info on this. (microsoft site) so that i can study it a bit more
AlanPosted Oct 8, 2008, 1:52 PM
It's probably because the two Panels are an integral part of the SplitContainer control rather than independent controls added to its Controls collection. Also they're of type SplitterPanel rather than Panel.
See if the following will work:
SplitContainer s1 = (SplitContainer)f1.Controls["splitContainer1"];
SplitterPanel p1 = s1.Panel1;
ProgressBar pb1 = (ProgressBar)p1.Controls["progressBar1"];
Label lb1 = (Label)p1.Controls["label1"];
pb1.Value = 50;
lb1.Text = "hi";
danie van ophemPosted Oct 8, 2008, 12:20 PM
thank you for aal you help. but ui still need to ask,
why doesent this work
Form1 f1 = (Form1)Application.OpenForms["Form1"]; SplitContainer s1 = (SplitContainer)f1.Controls["splitContainer1"]; Panel p1 = (Panel)s1.Controls["panel1"]; ProgressBar pb1 = (ProgressBar)p1.Controls["progressBar1"]; Label lb1 = (Label)p1.Controls["label1"];pb1.Value = 50;
lb1.Text =
"hi";AlanPosted Oct 7, 2008, 3:19 PM
To code the hypothetical example I mentioned in my previous post, you'd do something like this:
Form1 f1 = (Form1)Application.OpenForms["Form1"];
Panel p1 = (Panel)f1.Controls["panel1"];
TextBox tb = (TextBox)p1.Controls["textBox1"];
tb.Text += "whatever";
danie van ophemPosted Oct 7, 2008, 2:54 PM
I'm sorry to ask but how wold i do more than two steps. can you give me an example. I'm still new so i dont know all of the comon knowlege. :}
thanks
danie van ophemPosted Oct 7, 2008, 12:15 PM
Ah ok cool thanks. well if something els comes op about this i will ask :} thank you again :}
AlanPosted Oct 7, 2008, 12:10 PM
Yes, it'll work for any control on any open form in the current application.
Sometimes, though, you need to go through more than two steps. For example if you wanted to add text to a textbox on a panel within a form, then you'd need to get references to the form, panel and textbox before using the latter's Text property to add the text.
This is because the textbox would be in the panel's Controls collection rather than the form's.
danie van ophemPosted Oct 7, 2008, 12:01 PM
ah cool. thaks.
dows this work for every thing ???
AlanPosted Oct 7, 2008, 9:51 AM
Your class first needs a reference to your form (Form1 say) so that it can then get a reference to your ProgressBar(progressBar1 say) and update it.
If you're using .NET 2.0 or later, you can do it like this:
Form1 f1 = (Form1)Application.OpenForms["Form1"];
ProgressBar pb1 = (ProgressBar)f1.Controls["progressBar1"];
pb1.PerformStep();