Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I am preparing a small windows program in VC# in which I have prepared a few forms. Now my problem is I don't know how to close these forms.

I am trying the below code with no success:
1. this.hide(); // this code hides the form, but in background it keeps working.
2. this.close(); // this code exits the complete program
3.application.exit();  // this code also exits the complete program.

What I want is when I click on a button, only that particular form gets closed.

Thank you for your help!!!!
Posted
Updated 18-Feb-13 6:20am
v2
Comments
ali_heidari_ 18-Feb-13 12:46pm    
this.Dispose();

Always use Form.Close[^] method to close a form.

To close a particular form from its own class use
C#
this.Close()

if you want close a form from other form or class use
C#
formInstance.Close()

You should be careful here if you are going to use this form instance again in your code. because if you close the form all the resources created in the instance will be disposed and the instance is not useful any more. you have to create it again.

Application.Exit()[^] exits the whole application. You can call this method to exit your application at any point but its rarely used and not advised unless its serves its purpose. Always exit the application gracefully.
 
Share this answer
 
v2
Hi,

this.Close(); will close the entire application.
Even the usage of name_of_form.Close() won't work.
You need to use the below code i guess.
On the button click event,specify which form do u actually need to close and then create an instance of it, later calling the Close() method on that instance.
Application.OpenForms property gets a collection of open forms owned by the winform app.

C#
private void button2_Click(object sender, EventArgs e)
       {
           Form2 obj = (Form2)Application.OpenForms["Form2"];
           obj.Close();

       }


Hope it helps.

regards
anurag
 
Share this answer
 
Quote:
2. this.close(); // this code exits the complete program

No, if this is the main form, then the code exits the complete program (if this isn't the main form, then the particular form will be closed). If you want to execute some code after the form is closed, then go to your Program.cs file, and try this:
C#
static class Program
   {
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main()
       {
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);

           Application.Run(new Form1()); // in your Program.cs file, it may be another name than Form1
           // add HERE your code which you want to execute after your form is closed

       }
   }

If you don't add code after the Application.Run line, then nothing will be executed if the main form is closed, and then the application closes.

Also, C# is case-sensitive, so don't use this.close(), but this.Close(), also use this.Hide() instead of this.hide(), and use Application.Exit() instead of application.exit().

Hope this helps.
 
Share this answer
 
v5
As you have given in your example
this.close();
C# is a case sensitive language. You used a lower case in your code "close();", it may be the reason why you are getting somme errors.
Just use this:-
this.Close();
 
Share this answer
 
Hi
Just use name_of_form.Close() eg- form1.close()
 
Share this answer
 
Comments
jonlink01 18-Feb-13 12:38pm    
this code is giving me an error "'Form2' does not contain a definition for 'close'"
Richard C Bishop 18-Feb-13 12:41pm    
That is because it doesn't. The method is .Close().

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