Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Closing a form :)

Closing a form :)

Scheduled Pinned Locked Moved C#
tutorialquestion
11 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cristoff
    wrote on last edited by
    #1

    I cannot figure out how to ask the user for input if a form should be closed (or application exited), when alt+f4 or the X button is clicked? Is there an event being fired when the user clicks the X button (the one on the caption bar)?

    L C M 3 Replies Last reply
    0
    • C Cristoff

      I cannot figure out how to ask the user for input if a form should be closed (or application exited), when alt+f4 or the X button is clicked? Is there an event being fired when the user clicks the X button (the one on the caption bar)?

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Subscribe to the Form.Closing event :) top secret xacc-ide 0.0.1

      C 1 Reply Last reply
      0
      • C Cristoff

        I cannot figure out how to ask the user for input if a form should be closed (or application exited), when alt+f4 or the X button is clicked? Is there an event being fired when the user clicks the X button (the one on the caption bar)?

        C Offline
        C Offline
        Cristoff
        wrote on last edited by
        #3

        OK :) I have actually found out how it is done. Here is what a I did (nothing specially, really). I happens to me too often the solution is obvious but I miss it. buttonExit_Click... etc... is an event handler... is it how you are going to do it? Thanx

        	private void buttonExit\_Click(object sender, System.EventArgs e)
        	{
        		// Raises the System.Windows.Forms.Form.Closing event
        		this.OnClosing(new System.ComponentModel.CancelEventArgs());	
        	}
        	
        	private void FormOfficeTools\_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        	{
        		System.Windows.Forms.DialogResult result =
        			System.Windows.Forms.MessageBox.Show(this,
        			"Желаете ли да излезете от програмата?",
        			"Изход от програмата",
        			System.Windows.Forms.MessageBoxButtons.YesNo,
        			System.Windows.Forms.MessageBoxIcon.Question
        			);				 
        		if (System.Windows.Forms.DialogResult.Yes == result)
        			Application.Exit();
        		else
        		    // If "No" then cancel the System.Windows.Forms.Form.Closing event
        			e.Cancel = true;
        				
        	}
        	#endregion
        
        H 1 Reply Last reply
        0
        • L leppie

          Subscribe to the Form.Closing event :) top secret xacc-ide 0.0.1

          C Offline
          C Offline
          Cristoff
          wrote on last edited by
          #4

          sure but i've just missed that the System.ComponentModel.CancelEventArgs.Cancel property has to be set to true BTW is this call a good idea:

          this.OnClosing(new System.ComponentModel.CancelEventArgs());

          Here I raise the System.Windows.Forms.Form.Closing event, when a button Quit is clicked on (or another exit condition is met).

          R 1 Reply Last reply
          0
          • C Cristoff

            OK :) I have actually found out how it is done. Here is what a I did (nothing specially, really). I happens to me too often the solution is obvious but I miss it. buttonExit_Click... etc... is an event handler... is it how you are going to do it? Thanx

            	private void buttonExit\_Click(object sender, System.EventArgs e)
            	{
            		// Raises the System.Windows.Forms.Form.Closing event
            		this.OnClosing(new System.ComponentModel.CancelEventArgs());	
            	}
            	
            	private void FormOfficeTools\_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            	{
            		System.Windows.Forms.DialogResult result =
            			System.Windows.Forms.MessageBox.Show(this,
            			"Желаете ли да излезете от програмата?",
            			"Изход от програмата",
            			System.Windows.Forms.MessageBoxButtons.YesNo,
            			System.Windows.Forms.MessageBoxIcon.Question
            			);				 
            		if (System.Windows.Forms.DialogResult.Yes == result)
            			Application.Exit();
            		else
            		    // If "No" then cancel the System.Windows.Forms.Form.Closing event
            			e.Cancel = true;
            				
            	}
            	#endregion
            
            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            You don't need to call Application.Exit (in fact, I recommend you don't). The only condition you have to worry about is anything that would lead you to set CancelEventArgs.Cancel to true. If you don't set it, the form will continue closing as normal. The .NET Framework SDK even has a good example for the Form.Closing event.

            Microsoft MVP, Visual C# My Articles

            C 1 Reply Last reply
            0
            • H Heath Stewart

              You don't need to call Application.Exit (in fact, I recommend you don't). The only condition you have to worry about is anything that would lead you to set CancelEventArgs.Cancel to true. If you don't set it, the form will continue closing as normal. The .NET Framework SDK even has a good example for the Form.Closing event.

              Microsoft MVP, Visual C# My Articles

              C Offline
              C Offline
              Cristoff
              wrote on last edited by
              #6

              Thank you, Heath Stewart What's wrong with Application.Exit? Why don't you recommend using it? How should I then quit an application? I'm looking for the Form.Closing event... :) and still can't find it.

              H 1 Reply Last reply
              0
              • C Cristoff

                Thank you, Heath Stewart What's wrong with Application.Exit? Why don't you recommend using it? How should I then quit an application? I'm looking for the Form.Closing event... :) and still can't find it.

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                Because the form is already closing. If the form is your main form - the one you passed to Application.Run, once it's closed the application will exit anyway. There may already be messages in the pump that were posted that may need to clean-up resources. Calling Application.Exit circumvents that. So, if you don't set CancelEventArgs.Cancel to true, the form will close and the application will exit just as if you hadn't handled the Form.Closing event. And what do you mean you can't find it? It's documented in the .NET Framework SDK, and if you use the designer you can click the "Events" button in the PropertyGrid when your form is selected, find the Closing event and double-click it. You can also use IntelliSense. The thing is, you're not looking for Form.Closing, you're looking for this.Closing or _form1_.Closing or whatever is appropriate for accessing your instance of your form. Some basic understanding of programming is assumed. Closing is an instance event, so it will be defined for an instance of your form (so access it using this or a variable that refers to your Form).

                Microsoft MVP, Visual C# My Articles

                C 1 Reply Last reply
                0
                • H Heath Stewart

                  Because the form is already closing. If the form is your main form - the one you passed to Application.Run, once it's closed the application will exit anyway. There may already be messages in the pump that were posted that may need to clean-up resources. Calling Application.Exit circumvents that. So, if you don't set CancelEventArgs.Cancel to true, the form will close and the application will exit just as if you hadn't handled the Form.Closing event. And what do you mean you can't find it? It's documented in the .NET Framework SDK, and if you use the designer you can click the "Events" button in the PropertyGrid when your form is selected, find the Closing event and double-click it. You can also use IntelliSense. The thing is, you're not looking for Form.Closing, you're looking for this.Closing or _form1_.Closing or whatever is appropriate for accessing your instance of your form. Some basic understanding of programming is assumed. Closing is an instance event, so it will be defined for an instance of your form (so access it using this or a variable that refers to your Form).

                  Microsoft MVP, Visual C# My Articles

                  C Offline
                  C Offline
                  Cristoff
                  wrote on last edited by
                  #8

                  Sorry, I have meant the Form.Closing sample you have mentioned about and not the event! I have made a pretty stupid mistake and haven't checked what I have written. I understand about Application.Exit now. I thought that it isn't generally a good idea to use Application.Exit at all. BTW I have replaced Application.Exit with:

                  private void buttonExit_Click(object sender, System.EventArgs e)
                  {
                  this.Close(); // Close the main form and exit the application
                  }

                  Then I handle the Form.Closing event to ask the user if he really wants to quit etc. Is it a good idea or not? :) It seems to me like an implementation of your idea.

                  H 1 Reply Last reply
                  0
                  • C Cristoff

                    Sorry, I have meant the Form.Closing sample you have mentioned about and not the event! I have made a pretty stupid mistake and haven't checked what I have written. I understand about Application.Exit now. I thought that it isn't generally a good idea to use Application.Exit at all. BTW I have replaced Application.Exit with:

                    private void buttonExit_Click(object sender, System.EventArgs e)
                    {
                    this.Close(); // Close the main form and exit the application
                    }

                    Then I handle the Form.Closing event to ask the user if he really wants to quit etc. Is it a good idea or not? :) It seems to me like an implementation of your idea.

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #9

                    Yes, that is the way to handle it. Form.Close will send the Windows messages to close the Form. You're handling the WM_CLOSING message and optionally cancelling it. If you don't, the Form continues to close. When it's closed, if the message pump was waiting on it (i.e., you passed an instance of that Form to Application.Run), then the application will exit.

                    Microsoft MVP, Visual C# My Articles

                    1 Reply Last reply
                    0
                    • C Cristoff

                      sure but i've just missed that the System.ComponentModel.CancelEventArgs.Cancel property has to be set to true BTW is this call a good idea:

                      this.OnClosing(new System.ComponentModel.CancelEventArgs());

                      Here I raise the System.Windows.Forms.Form.Closing event, when a button Quit is clicked on (or another exit condition is met).

                      R Offline
                      R Offline
                      Roman Rodov
                      wrote on last edited by
                      #10

                      to Raise the Form.Closing event you need to just call: this.Close();

                      1 Reply Last reply
                      0
                      • C Cristoff

                        I cannot figure out how to ask the user for input if a form should be closed (or application exited), when alt+f4 or the X button is clicked? Is there an event being fired when the user clicks the X button (the one on the caption bar)?

                        M Offline
                        M Offline
                        mikker_123
                        wrote on last edited by
                        #11

                        Yes there is... it's called Closing... and when you handle it you can do all kind of stuff (including cancellation of closing) with it ;)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups