Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
hi,
i have a button in form1, initially in form1_load() i disabled that button. At the end of the certain operations which was done in form2, i need to enable the button in form1.
How to do this?

I created object for form1 and tried to enables it, in form2 like..

form2_load()
{
...
...
Form1 a=new Form1();
a.button1.enable=true;
}


But it not works.
Posted
Updated 24-May-11 6:07am
v2
Comments
Sandeep Mewara 24-May-11 9:37am    
Hint: You are creating a new instance of form1 instead of using the existing one by doing:
Form1 a=new Form1();

Hi,

1. Add new constructor in Form2 and add parameter of Form1.
From1 frm1;
public Form2()
{}
public Form2(Form1 refOfForm1)
{
  frm1 = refOfForm1;
}


2. Pass parameter of Form1 and show Form2 from Form1.
  private void showform2_click(Object sender,EventArgs e)
  {
    Form2 f2 = new Form2(this);
f2.show();
  }


3. Call any method of Form1 from Form2 using 'frm1' object.
private void enableBtnInForm1()
{
  frm1.EnableBtn();
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 24-May-11 16:37pm    
Not the most robust one.
Please see my answer.
--SA
Sergey Alexandrovich Kryukov 23-Jun-14 2:26am    
Did you see my answer? Please, if this is not enough, comment on my question. If it is needed, I'll post more detailed answer, with such sample.
—SA
This is a popular question. The most robust way of form cooperation is implementing some interface in the form class.

Please see my past answer here, see also other solutions and the discussion:
How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
Comments
sairam.bhat 25-May-11 2:21am    
ya sir my +5
sairam.bhat 25-May-11 2:22am    
my +5
Sergey Alexandrovich Kryukov 25-May-11 2:51am    
Thank you very much.
--SA
Answered only to move it from the Unanswered list: OP found solution.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-11 16:37pm    
Not the best one.
Please see my answer.
--SA
yes...i got a solution



http://www.codeproject.com/Questions/119349/how-to-enable-the-buttons-which-are-in-parent-form.aspx
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-11 16:36pm    
May be not the best one. Please see the most comprehensive way I describe in my answer (referenced).
--SA

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