Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 18-Feb-13 6:17am
Edited 18-Feb-13 6:20am
v2
Comments
source.compiler at 18-Feb-13 12:46pm
   
this.Dispose();
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
 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.
  Permalink  
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
Just use name_of_form.Close() eg- form1.close()
  Permalink  
Comments
jonlink01 at 18-Feb-13 12:38pm
   
this code is giving me an error "'Form2' does not contain a definition for 'close'"
richcb at 18-Feb-13 12:41pm
   
That is because it doesn't. The method is .Close().
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
 
private void button2_Click(object sender, EventArgs e)
       {
           Form2 obj = (Form2)Application.OpenForms["Form2"];
           obj.Close();
 
       }
 
Hope it helps.
 
regards
anurag
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Always use Form.Close[^] method to close a form.
 
To close a particular form from its own class use
this.Close()
if you want close a form from other form or class use
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.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100