Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
How can I hide a button on the Main Form from the second form? I tried to write the following code but it is not working:

//Main Form
private void button1_Click(object sender, EventArgs e)
        {
            frmtwo formtwo = new frmtwo();
            formtwo.ShowDialog();
        }


//Form Two
private void button1_Click(object sender, EventArgs e)
       {
           frmMain  formMain = new frmMain();
           formMain.button1.Hide();
           this.Close();
       }
Posted

No, that wont work - you need the current instance of the main form, not a new one.
What you have tried to do is put your mobile phone in the glove box of your car, then expect to be able to open the glove box in my car and find your phone.

It is possible - if you hand an instance of the main form to the second when you open it, but it is a very bad idea - it locks the design of the two forms together, so you can't use form2 without the main form, or change the main form without considering teh effects on form2. This is against the principles of OOP and considered very bad practice.

Instead, construct an event in form2 which the main form subscribes to when it creates it. When the event is signalled, the main form can then hide it's own button, or take whatever other measures it sees fit.

It's pretty easy to do. In form2:
C#
// Signal operation disabled
public event EventHandler DisableOperation;

protected virtual void OnDisableOperation(EventArgs e)
   {
   EventHandler eh = DisableOperation;
   if (eh != null)
      {
      eh(this, e);
      }
   }

private void DoSomethingToDisableOperation()
   {
   OnDisableOperation(null);
   }
In your main form:
C#
private void ShowChildForm()
    {
    // Link in his event so if it changes, we detect it.
    frmChild fd = new frmChild();
    fd.DisableOperation += new EventHandler(DisableOperation);
    fd.ShowDialog();
    }

//
// Fired when the file is changed at a lower level.
//
private void DisableOperation(object sender, EventArgs e)
    {
    myButton.Enabled = false;
    }
 
Share this answer
 
Comments
Shahin Khorshidnia 29-Feb-12 4:59am    
+5
JacoBosch 29-Feb-12 5:48am    
Thank You! Your explanation is really good!
The problem here is that you are not working with the original instance of the main form, you are just working with a new instance. You have to have the reference to your main form in your second form. You declare a member (of typemain form) and set it to the original instance of the main form, either through a property or an overloaded constructor and then hide the button using the member variable.
 
Share this answer
 
Comments
OriginalGriff 29-Feb-12 4:49am    
That is a very bad idea - it ties the two forms together and makes it difficult to maintain. Consider using an event instead.
[no name] 29-Feb-12 8:48am    
I was just giving him a heads up on what was causing the problem. I didn't think of explaining design guidelines :-)
OriginalGriff 29-Feb-12 9:03am    
The problem is that with a beginner if you give them a solution at all, they assume it is the right thing to do. And then because it worked, they always use it in future.
Then you or I end up trying to sort out the code when they leave the company...:laugh:
[no name] 29-Feb-12 13:09pm    
Agree, will keep in mind for future :-)
You now create a new instance of the mainForm and you want to get a reference to the existing instance. You have several options. To name a few:
1. You can add a property to Form2 for specifying the form when creating formtwo and use that. ie:
formtwo.some_form = frmMain;

2. You can get the form using Application.OpenForms, ie (assuming the mainform is at index 0:
Application.OpenForms[0]


Good luck!
 
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