473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

JnrJnr
88
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
Oct 11 '09 #1
9 2859
tlhintoq
3,525 Expert 2GB
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
Oct 11 '09 #2
tlhintoq
3,525 Expert 2GB
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.  
.
Oct 11 '09 #3
tlhintoq
3,525 Expert 2GB
Here is another thread with virtually the same discussion and some advice.

http://bytes.com/topic/c-sharp/answe...e-form-another
Oct 11 '09 #4
JnrJnr
88
@tlhintoq

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?
Oct 11 '09 #5
JnrJnr
88
@tlhintoq

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();
Oct 11 '09 #6
tlhintoq
3,525 Expert 2GB
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.


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"
Oct 11 '09 #7
JnrJnr
88
@tlhintoq
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?
Oct 17 '09 #8
tlhintoq
3,525 Expert 2GB
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.
Oct 19 '09 #9
JnrJnr
88
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));
Nov 1 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: MJW | last post by:
Is there a way for me to know if or which command button was just clicked that triggers the LostFocus event for the current control on a Form? I have a form that has many types of users who each...
2
by: kma | last post by:
I am designing an Access 2000 database on a computer running Windows 98. I have one form with several tabs; all of which have sub forms, some with a subform on a subform. For example, on my...
2
by: Almir | last post by:
I have a simple problem, i just can get a grasp on it. I designed a database for inventory of computer equipment. Now i created forms for each table. Each piece of equipment is going to come in an...
2
by: Paul Malcomson | last post by:
Hi. I'm having terrible trouble with a form that displays several parent/child relationships at one time. It is a sales force hierarchy - Sales force, district, territory, sales rep are the...
9
by: Melissa | last post by:
What is the code to delete a command button from a form? Can the code be run from the click event of the button to be deleted? Thanks! Melissa
5
by: campbellbrian2001 | last post by:
I'm trying to get the "Carry data over to new record" code to work from Allen Browne's site: http://allenbrowne.com/ser-24.html I follwed the instruction explicitly and somethings not working......
14
by: Kevin | last post by:
A couple of easy questions here hopefully. I've been working on two different database projects which make use of multiple forms. 1. Where's the best/recommended placement for command buttons...
2
by: XML newbie: Urgent pls help! | last post by:
If I get SessionID in 1 function how do I carry that SessionID(value of this SessionID) to another function or another form within the same project
3
chunk1978
by: chunk1978 | last post by:
hi there... i'm trying to make one text field contain the value of another... i'm assuming there's a really simple solution... here's a code i quickly wrote to further explain my issue: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.