Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i access a control on form 1 from a button click event on form 2?
am trying to fill a combo box on form one with the updated data. The data is updated on form two. how do i go about doing this?

this is the code am using

mainForm mf = new mainForm();
mf.intdept.Items.Clear();
Posted
Updated 18-Jun-11 8:08am
v2

You could:

0) Create a custom event that is fired when the form2 button is clicked, and let form1 hook that event. This way, you can create your own EventArgs derived object and the event will pass whatever you want back to the form1 event handler.

1) Make the button in form2 public and after the form is created, add an event handler in form1 for the button in form2.

2) Make the variable/control in form1 public, pass the form1 object to form2, and let form2 do the work.

Of the three ways, I think you should do the first one.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jun-11 20:41pm    
I think my method using interface is better, please see.
--SA
This is a popular question about form collaboration. The most robust method is implementing appropriate interface in form class.

Please see my past solution:
How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
in Form1 :

Create 1 Label control set text as 'Form1'

Label1.Modifier=select as PUblic


in Form2

place Button1:

code under button1_click event of Form2 :

Form1 obj = new Form1();
obj.button1.Text = "raise";
obj.Show();
 
Share this 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