Click here to Skip to main content
15,867,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi ,,

Q1: i want to disable all button controls in my windows form ,,

is that any solution with Foreach Statement ?
OR
any solution !!


Q2: how can i Access to my any control in another form ?! for ex ,, i load Form2 and i want to change a text of label in Form 1 !?
How can i do it !?
Posted

Q1: Disable is relatively easy:
C#
foreach (Control c in Controls)
   {
   Button b = c as Button;
   if (b != null)
      {
      b.Enabled = false;
      }
   }
If you use any containers such as Panels, you may want to put this in a method and use recursion through each control.Controls collection to get the buttons within them.


Q2: Don't! It is possible, but it is a very bad practice because it is against the principles of OOPs. When you access controls on a different form directly, you tie the design of the two forms together, so you can't change one without considering the effects on the other - this also makes it a lot harder to reuse your code which makes maintenance a PITA.

Instead, use a property on the target form which does the actual work, and change that from the other form. The complication comes if you want to change the value on a parent form, because you should not even know the parent exists! In that case, you should create an event in the child which the parent handles to say "new data available" and a property in the child which supplies the new string. The parent then gets the new text and sets it's own label.
 
Share this answer
 
Comments
mahmii 17-Feb-13 6:10am    
tnx man ,, and another Question ...
i want to Close my Form1 when i load my Form2 !
look :

Form2 f2=new Form2();
f2.show();
//and then Close my Main Form( Form1)
but when i close my Form1 (this.Close())
the both of them(from1 & form2) are close
pls give me a little help !! i know my English is not good but any way i try to improve that :D
OriginalGriff 17-Feb-13 6:31am    
You can't close form1 in this case - it is the main form for the application, and when it closes, so does the app.
Instead, use Hide to make it disappear, then use ShowDialog to show the form2. When that returns, Close the form1 (or make it visible again).
Form2 f2=new Form2();
Hide();
f2.ShowDialog();
Close();
mahmii 17-Feb-13 9:55am    
tnx man ,, i like your solution's and one more Question :D
how can i viiable Form1 again when im coding in From2.cs?!?!

for ex : consider i have a button in my form2 for saving data in database ,, now my data are save correctly and now i want to close form2 and then open my form1 again !!!
you now for many years i worked with asp.net concepts like ( Query string , handlers , post load ...) and now ... i Confused !!! i hate winapp >:(
OriginalGriff 17-Feb-13 10:51am    
:laugh:
Call Show instead of Close!
Form2 f2=new Form2();
Hide();
f2.ShowDialog();
Show();
OriginalGriff 17-Feb-13 10:53am    
WinForms is a lot simpler to deal with than web based - you don't have to mess about with Sessions, cookies, postbacks etc. It's just a case of getting your head round a slightly different model, is all.

And it's a whole *shed load* easier to debug! :laugh:
i didnt get u clealy, but from what i have understood, here is a method:

C#
Form ob = new form2();
foreach (Control c in ob.Controls)
 {
   if (c is Label)
     {
        Label lb = (Label)c;
       //if u want to change text of some perticular label in that form then:
       //if(lb.Name.Equals("label's name"))
      lb.Text = "new text";
      }
 }


this will change the text of form2's label control
 
Share this answer
 
Comments
DaveyM69 17-Feb-13 7:32am    
The OP wants it the other way round, change Label's text in form 1 from form 2.
There are two ways to do this - constructor injection or an event. An event it the preferred and cleanest (most OOP) way. Refer to OriginalGriff's answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900