Connecting Tech Pros Worldwide Help | Site Map

How do I carry out a command for a control on one form to another

JnrJnr's Avatar
Newbie
 
Join Date: Oct 2009
Location: South Africa
Posts: 6
#1: Oct 11 '09
Im righting in Visual C sharp and want to carry out commands for controls(buttons,labels) from form2 onto form1.
For instance: I want to to use form1 as my main form...form1 must have a label and form2 must have a button.
When the button on form2 is clicked I want the label on form1 to be disabled.
I just want to know how to interact with controls on one form to another.

Your help would be much appreciated
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#2: Oct 11 '09

re: How do I carry out a command for a control on one form to another


Its preferable to NOT interact with controls directly.
It ties the two forms tightly to each other and makes it harder to maintain.
For example, if you decide 6 months from not to rename the label from Label1 to lblFirstName to make it more descriptive the code in Form2 breaks.

The preferred way would be to have Form1 subscribe to events in Form2.
This way you can raise an event of "btnClearClicked" in Form2.
Form2 doesn't care who is listening. It just yells out that the button was clicked. Now if another form, or 10 other forms are listening they will all react at the same time to the same event.

It makes Form1 responsible for its own actions (and re-actions), instead of button the burden of Form1 behavior onto Form2
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#3: Oct 11 '09

re: How do I carry out a command for a control on one form to another


With all that said...To do it they way you've described (unrecommended)

What you've described makes me guess that Form2 really doesn't know who Form1 is. Form2 is probably the child of Form1, meaning that Form1 was was created, then Form1 probably made Form2. Am I guessing right?

Since Form2 doesn't know who Form1 is, then Form2 doesn't know about any of the controls of Form1. But, Since Form1 created Form2 it *does* know about it and its controls. So make the button on Form2 public, then on Form1 make a new handler for it

Inside Form1 somethng like this
Expand|Select|Wrap|Line Numbers
  1. Form2 myForm2 = new Form2();
  2. Form2.btnOne.Click += new btnOne_Click(btnOneHandlerMethod);
  3.  
.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#4: Oct 11 '09

re: How do I carry out a command for a control on one form to another


Here is another thread with virtually the same discussion and some advice.

Accessing Components of One Form From Another
JnrJnr's Avatar
Newbie
 
Join Date: Oct 2009
Location: South Africa
Posts: 6
#5: Oct 11 '09

re: How do I carry out a command for a control on one form to another


Quote:

Originally Posted by tlhintoq View Post

With all that said...To do it they way you've described (unrecommended)

What you've described makes me guess that Form2 really doesn't know who Form1 is. Form2 is probably the child of Form1, meaning that Form1 was was created, then Form1 probably made Form2. Am I guessing right?

Since Form2 doesn't know who Form1 is, then Form2 doesn't know about any of the controls of Form1. But, Since Form1 created Form2 it *does* know about it and its controls. So make the button on Form2 public, then on Form1 make a new handler for it

Inside Form1 somethng like this

Expand|Select|Wrap|Line Numbers
  1. Form2 myForm2 = new Form2();
  2. Form2.btnOne.Click += new btnOne_Click(btnOneHandlerMethod);
  3.  
.


Thank you for the reply.
Yes you guessed right..in form1 I declared a new form, I have not made any parent or child forms. I prefer to do it the recomended way as long as I can get the outcome I need. How do I make form1 subscrbe to events in form2?
JnrJnr's Avatar
Newbie
 
Join Date: Oct 2009
Location: South Africa
Posts: 6
#6: Oct 11 '09

re: How do I carry out a command for a control on one form to another


Quote:

Originally Posted by tlhintoq View Post

With all that said...To do it they way you've described (unrecommended)

What you've described makes me guess that Form2 really doesn't know who Form1 is. Form2 is probably the child of Form1, meaning that Form1 was was created, then Form1 probably made Form2. Am I guessing right?

Since Form2 doesn't know who Form1 is, then Form2 doesn't know about any of the controls of Form1. But, Since Form1 created Form2 it *does* know about it and its controls. So make the button on Form2 public, then on Form1 make a new handler for it

Inside Form1 somethng like this

Expand|Select|Wrap|Line Numbers
  1. Form2 myForm2 = new Form2();
  2. Form2.btnOne.Click += new btnOne_Click(btnOneHandlerMethod);
  3.  
.


I made button1 in form2 public and declared a new form(form2) in form1 and
I dont seem to find the .btnOne in Form2.btnOne.Click += new btnOne_Click(btnOneHandlerMethod); popping up in the intelisense.
Did I create form2 wrong? I just added a new form in the solution explorer and in form1 I refered to form2 as Form2 myform = new Form2();
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#7: Oct 11 '09

re: How do I carry out a command for a control on one form to another


Quote:

Originally Posted by Jnrjnr

Yes you guessed right..in form1 I declared a new form, I have not made any parent or child forms.

Form1 would be the parent.
Form2 would be the child, because it was made by Form2. Just like with people. If you make a new person, you are the parent, it is the child. This is why the parent form knows everything about the child, but not the other way around. A parent knows who the child is, and all of its properties like "Eyes = Color.Green". Yet a child doesn't necessarily know who its parent is or any of its properties.


Quote:

Originally Posted by jnr

I dont seem to find the .btnOne in Form2.btnOne.Click += new btnOne_Click(btnOneHandlerMethod);

That was an example.
You have substitute the actual names and events from your project. I don't know if you named your button "Button2", btnTwo, "buttonSave"

In my example the button was named "btnOne"
In my example the custom event (that you need to make) was named "Click"

It could just as easily be...
Expand|Select|Wrap|Line Numbers
  1. Form2 myCustomerForm = new Form2();
  2. myCustomerForm.btnSave.SaveNow += new myCustomer.Form.SaveNow(SaveToFile);
  3.  
... if you created an event named "SaveNow" and a method in your form of "SaveToFile"
JnrJnr's Avatar
Newbie
 
Join Date: Oct 2009
Location: South Africa
Posts: 6
#8: Oct 17 '09

re: How do I carry out a command for a control on one form to another


Quote:

Originally Posted by tlhintoq View Post

Form1 would be the parent.
Form2 would be the child, because it was made by Form2. Just like with people. If you make a new person, you are the parent, it is the child. This is why the parent form knows everything about the child, but not the other way around. A parent knows who the child is, and all of its properties like "Eyes = Color.Green". Yet a child doesn't necessarily know who its parent is or any of its properties.



That was an example.
You have substitute the actual names and events from your project. I don't know if you named your button "Button2", btnTwo, "buttonSave"

In my example the button was named "btnOne"
In my example the custom event (that you need to make) was named "Click"

It could just as easily be...

Expand|Select|Wrap|Line Numbers
  1. Form2 myCustomerForm = new Form2();
  2. myCustomerForm.btnSave.SaveNow += new myCustomer.Form.SaveNow(SaveToFile);
  3.  
... if you created an event named "SaveNow" and a method in your form of "SaveToFile"

Thank you I understand the whole concept of parent and child forms now and I understand what the line of code does but I havent gotten it right for my custom named event of my named button to display in intelisence (in your example the "SaveNow" event). Also I made my named button public in the form's Designer.cs is this the right way of making it public?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#9: Oct 19 '09

re: How do I carry out a command for a control on one form to another


Quote:
Also I made my named button public in the form's Designer.cs is this the right way of making it public?
Not exactly. You still have the same problem that the button control itself has to be exposed, and every other form needs to know if it's existence to use it.

The form should have an event called "SaveNow", not that the button has an event called "SaveNow"

You don't care how the event is thrown. Maybe when someone clicks a button: That's a good time to save. Maybe when values of the form have been changed: That's a good time to save. Maybe when a timer goes off so you autosave every 10 minutes: That's a good time to save.

You only care that the FORM wants to save, not that the button was clicked.
JnrJnr's Avatar
Newbie
 
Join Date: Oct 2009
Location: South Africa
Posts: 6
#10: 3 Weeks Ago

re: How do I carry out a command for a control on one form to another


Hi there sorry for the late reply. I just want to say thanks for the help. I have figured it out...the way you explained.
  1. Form2 myForm = new Form2
  2. myForm.show();
  3. myForm.btn1.click += new EventHandler(changeLabelText);

Under the click event....
  1. BeginInvoke (new EventHandler(changeLabelText));
Reply