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
  1. Home
  2. General Programming
  3. C#
  4. How did the user close the form?

How did the user close the form?

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 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
    Chals
    wrote on last edited by
    #1

    Hi, I'm doing a windows app and I need to know if the user pushed the red X button in the top right of my form. I know the Form_closing has an enum telling the reason of the form close, but it doesnt specify if it was my own cancel button or the X button, and I need to know it. Any ideas? Thanks in advance

    M 1 Reply Last reply
    0
    • C Chals

      Hi, I'm doing a windows app and I need to know if the user pushed the red X button in the top right of my form. I know the Form_closing has an enum telling the reason of the form close, but it doesnt specify if it was my own cancel button or the X button, and I need to know it. Any ideas? Thanks in advance

      M Offline
      M Offline
      malharone
      wrote on last edited by
      #2

      public class MyForm:Form { private bool _cancelButtonClicked = false; private void cancelbutton_clicked(..) { _cancelButtonClicked = true; } private void form_closing(..) { if (_cancelButtonClicked) { //User clicked the button with the dialog result == cancel } else { //User clicked X button to close the form } } } - Malhar

      C 1 Reply Last reply
      0
      • M malharone

        public class MyForm:Form { private bool _cancelButtonClicked = false; private void cancelbutton_clicked(..) { _cancelButtonClicked = true; } private void form_closing(..) { if (_cancelButtonClicked) { //User clicked the button with the dialog result == cancel } else { //User clicked X button to close the form } } } - Malhar

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

        Yeah, well...thanks for the reply, but I was looking for a non-flag solution, thats the easy way :)

        D 1 Reply Last reply
        0
        • C Chals

          Yeah, well...thanks for the reply, but I was looking for a non-flag solution, thats the easy way :)

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          That's also the only way you can tell the difference. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          C 1 Reply Last reply
          0
          • D Dave Kreskowiak

            That's also the only way you can tell the difference. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            C Offline
            C Offline
            Chals
            wrote on last edited by
            #5

            Damn...thats bad. Thanks for your help guys

            M 1 Reply Last reply
            0
            • C Chals

              Damn...thats bad. Thanks for your help guys

              M Offline
              M Offline
              microsoc
              wrote on last edited by
              #6

              hi chals! :) try this:

              private const int WM_SYSCOMMAND = 0x112;
              private const int SC_CLOSE = 0xF060;

              protected override void WndProc(ref Message m)
              {
              if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE)
              {
              //do something here
              }
              }

              messages under WM_SYSCOMMAND includes: - Restore, Move, Size, Minimize, Maximize & Close (x button in the forms) or the commands in the context menu when you right click the title bar of a window. the SC_CLOSE WParam is for the close command. you can look for other commands in the WinUser.h file of Visual Studios. most of the time it is in "C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\" folder. It has the list of constants for API commands. hope that helps! :) microsoc :cool:

              C 1 Reply Last reply
              0
              • M microsoc

                hi chals! :) try this:

                private const int WM_SYSCOMMAND = 0x112;
                private const int SC_CLOSE = 0xF060;

                protected override void WndProc(ref Message m)
                {
                if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE)
                {
                //do something here
                }
                }

                messages under WM_SYSCOMMAND includes: - Restore, Move, Size, Minimize, Maximize & Close (x button in the forms) or the commands in the context menu when you right click the title bar of a window. the SC_CLOSE WParam is for the close command. you can look for other commands in the WinUser.h file of Visual Studios. most of the time it is in "C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include\" folder. It has the list of constants for API commands. hope that helps! :) microsoc :cool:

                C Offline
                C Offline
                Chals
                wrote on last edited by
                #7

                Thanks!!! That worked like a charm

                M 1 Reply Last reply
                0
                • C Chals

                  Thanks!!! That worked like a charm

                  M Offline
                  M Offline
                  microsoc
                  wrote on last edited by
                  #8

                  nice to know that! :) happy coding! :) microsoc :cool:

                  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