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. What message is called when the X in the upper right of a dialog is closed

What message is called when the X in the upper right of a dialog is closed

Scheduled Pinned Locked Moved C#
4 Posts 3 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.
  • D Offline
    D Offline
    Dan Neely
    wrote on last edited by
    #1

    I need to do some cleanup when my dialog closes. I tried overriding mydialog.Close() doing the cleanup there and then calling base.Close(). When the user exits it using the close button everything works as intended, but the X doesn't call Close() and leaves my app in a messedup state.

    M M 2 Replies Last reply
    0
    • D Dan Neely

      I need to do some cleanup when my dialog closes. I tried overriding mydialog.Close() doing the cleanup there and then calling base.Close(). When the user exits it using the close button everything works as intended, but the X doesn't call Close() and leaves my app in a messedup state.

      M Offline
      M Offline
      Mohamad Al Husseiny
      wrote on last edited by
      #2

      The Windows recive WM_SYSCOMMAND Notification when you close minimize or maximize the window or you choose any command from the system menu So overrid your form WndProc function and handle it

      const int WM_SYSCOMMAND  = 0x0112;
      const int SC_CLOSE    =    0xF060;
      

      protected override void WndProc(ref Message m)
      {
      if(m.Msg==WM_SYSCOMMAND)
      {
      if(m.WParam.ToInt32()==SC_CLOSE)
      {
      MessageBox.Show("Application will be close");
      // call the default after you handle it
      base.WndProc(ref m);
      }
      }
      else
      {
      base.WndProc (ref m);
      }
      }

      if you don not handle other Message you can write it like

      protected override void WndProc(ref Message m)
      {
      if(m.Msg==WM_SYSCOMMAND)
      {
      if(m.WParam.ToInt32()==SC_CLOSE)
      {
      MessageBox.Show("Application will be close");
      }

      }
          base.WndProc (ref m);
      

      }

      for more information look at WM_SYSCOMMAND Notification[^] MCAD -- modified at 18:53 Friday 23rd September, 2005

      1 Reply Last reply
      0
      • D Dan Neely

        I need to do some cleanup when my dialog closes. I tried overriding mydialog.Close() doing the cleanup there and then calling base.Close(). When the user exits it using the close button everything works as intended, but the X doesn't call Close() and leaves my app in a messedup state.

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        You should handle the Closing event of System.Windows.Forms.Form. This event is fired regardless of whether the dialog was closed via the system menu or a close button on your form. Regards, mav

        D 1 Reply Last reply
        0
        • M mav northwind

          You should handle the Closing event of System.Windows.Forms.Form. This event is fired regardless of whether the dialog was closed via the system menu or a close button on your form. Regards, mav

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #4

          *blush* In my defense I can only say Friday 4pm.

          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