Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
how to close one form through another form by button click.

I tried using the below code , but it doesn't work:

C#
private void btnInventory_Click(object sender, EventArgs e)
{           
            frmCustomer f = new frmCustomer();
            f.Close();
           
}


What is the right way ?

Appreciate your feedback.
Thanking You
Posted
Updated 16-Apr-20 0:12am

Hi,

frmCustomer f = new frmCustomer();
f.Close();


can not work since this "f" is created locally. You need to find the existing instance of "frmCustomer" and then close this instead of creating a new instance to close.

What your code could look like:

frmCustomer f;

private void btnShowDialog_Click(object sender, EventArgs e)
{
  if(f == null)
    f = new frmCustomer();
  
  f.Show();
}

private void btnInventory_Click(object sender, EventArgs e)
{
  if(f != null)
    f.Close();
}
 
Share this answer
 
The code you tried will never work. You create a new object of frmCustomer and close it before ever opening it. You must understand that when you apply new you create a completely new object. This object is of the same class of an earlier created object but the data is not the same. Therefore you cannot control the other objects of the same class by simply using another instance of that object.

To close this Form you would need the actual reference to that Form. So you would need to give this to your frmInventory (you could for example add a property where you can set this reference) and use that to close the other Form.

Another way is to look for that Form in all open Forms of your Application like this:
C#
for (int index = Application.OpenForms.Count-1; index>= 0; index--)
{
    if (Application.OpenForms[1].Name == "Customer")
    {
        Application.OpenForms[1].Close;
    }
}

Be aware that using a foreach like this can result in an Exception:
C#
foreach (Form form in Application.OpenForms)
{
    // ...
}

This is because you altered the list within the loop which might give you an invalid operation exception.

Good luck!
 
Share this answer
 
v3
Comments
shanawazway 27-Oct-10 8:26am    
Thanks for best answer.
Here I made one change in your For Loop : index++ => index--
It went into infinite Loop
Before:
for (int index = Application.OpenForms.Count-1; index>= 0; index++)

After:
for (int index = Application.OpenForms.Count-1; index>= 0; index--)


Thanks Again.
Member 12810663 16-Apr-20 9:06am    
simple solution

if (Application.OpenForms[1].Name == "Customer")
{
Application.OpenForms[1].Close;
}
E.F. Nijboer 27-Oct-10 11:48am    
Yes, your correction is completely correct. Except I do not see the change in the answer but I'll update it so it will be correct.
Sunasara Imdadhusen 28-Oct-10 6:02am    
Good Answer!
The clue is in the word:
MIDL
frmCustomer f = new frmCustomer();
f.Close();
What does the new keyword do?

Would you expect my car key to fit your car ignition? Would you be annoyed if it did and I drove away?

So why do you expect that closing a new instance of a form to close a different, existing instance?

Find the instance of the existing form, and close that. Where did you open it? Look there, and hopefully that means you will have saved the instance somewhere. If not, you will have to. Or get creative with the Application object - better to save the instance, really.
 
Share this answer
 
frmCustomer f = new frmCustomer();
this created a new object and not the object you actually mean to close

suppose there are 2 forms
Form1 and Form2
in form1 if you are creating an object (f2) of Form2
you can do whatever you please with the object (f2).
But from Form2 you canot access the Form1.
in order to access the first form you have to pass through a parameter, function, property or constructor
hope it helps
 
Share this answer
 
Hi I had the same problem.

But in my case i had to close main form from within my custom button class, so i did...


this.Parent.Dispose();

this(My custom button class)

parent(the form or the container where the button is placed on)

or Application.Exit(); //Closes the hole program.

I hope this helped, Best regards
 
Share this answer
 
v2
Comments
Kats2512 23-Jul-18 3:19am    
8 year old question that has already been answered.

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