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. Disabling of Close Form option

Disabling of Close Form option

Scheduled Pinned Locked Moved C#
tutorial
9 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.
  • N Offline
    N Offline
    Neo Andreson
    wrote on last edited by
    #1

    How to disable Form close option (X Box).

    Truth Is The Simplest !!!!

    P C 2 Replies Last reply
    0
    • N Neo Andreson

      How to disable Form close option (X Box).

      Truth Is The Simplest !!!!

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Handle the Closing event? But, really, other than asking "Are you sure?" you should allow the user to close the application.

      1 Reply Last reply
      0
      • N Neo Andreson

        How to disable Form close option (X Box).

        Truth Is The Simplest !!!!

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Why not just remove it ? You can do that in the form properties in the designer

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        N 1 Reply Last reply
        0
        • C Christian Graus

          Why not just remove it ? You can do that in the form properties in the designer

          Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

          N Offline
          N Offline
          Neo Andreson
          wrote on last edited by
          #4

          ThankXXX..Christian Yes i want to remove but there is no property available. If it is there pls let me know.

          Truth Is The Simplest !!!!

          C D 2 Replies Last reply
          0
          • N Neo Andreson

            ThankXXX..Christian Yes i want to remove but there is no property available. If it is there pls let me know.

            Truth Is The Simplest !!!!

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            It's the 'Control Box' option.

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            1 Reply Last reply
            0
            • N Neo Andreson

              ThankXXX..Christian Yes i want to remove but there is no property available. If it is there pls let me know.

              Truth Is The Simplest !!!!

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              Removing the control box gets rid of minimize and maximize too. This method just disables the close button and the Close menu item in the form's context menu. Make sure that you give the user a clean way to close the form though!

              public Form1()
              {
              InitializeComponent();
              EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_GRAYED);
              }
              private const int SC_CLOSE = 0xf060;
              private const int MF_GRAYED = 0x0001;
              [System.Runtime.InteropServices.DllImport("user32.dll")]
              private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
              [System.Runtime.InteropServices.DllImport("user32.dll")]
              private static extern int EnableMenuItem(IntPtr hMenu, int uIDEnableItem, int uEnable);

              Edit: I should add - to enable it again if you need to declare this constant:

              private const int MF_ENABLED = 0x0000;

              and the code to perform the enable is:

              EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_ENABLED);

              Dave

              modified on Friday, February 22, 2008 6:14 AM

              P 1 Reply Last reply
              0
              • D DaveyM69

                Removing the control box gets rid of minimize and maximize too. This method just disables the close button and the Close menu item in the form's context menu. Make sure that you give the user a clean way to close the form though!

                public Form1()
                {
                InitializeComponent();
                EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_GRAYED);
                }
                private const int SC_CLOSE = 0xf060;
                private const int MF_GRAYED = 0x0001;
                [System.Runtime.InteropServices.DllImport("user32.dll")]
                private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
                [System.Runtime.InteropServices.DllImport("user32.dll")]
                private static extern int EnableMenuItem(IntPtr hMenu, int uIDEnableItem, int uEnable);

                Edit: I should add - to enable it again if you need to declare this constant:

                private const int MF_ENABLED = 0x0000;

                and the code to perform the enable is:

                EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_ENABLED);

                Dave

                modified on Friday, February 22, 2008 6:14 AM

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Just tested it; it works, but Alt+F4 still closes the form.

                D 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Just tested it; it works, but Alt+F4 still closes the form.

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Forgot that one! The code below handles that :-D

                  private const int WM_SYSKEYDOWN = 0x0104;
                  private bool handleAltF4 = true;
                  protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
                  {
                  if (msg.Msg == WM_SYSKEYDOWN)
                  {
                  switch (keyData)
                  {
                  case System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.F4:
                  {
                  return handleAltF4;
                  }
                  }
                  }
                  // process all others as normal
                  return false;
                  }

                  Dave

                  P 1 Reply Last reply
                  0
                  • D DaveyM69

                    Forgot that one! The code below handles that :-D

                    private const int WM_SYSKEYDOWN = 0x0104;
                    private bool handleAltF4 = true;
                    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
                    {
                    if (msg.Msg == WM_SYSKEYDOWN)
                    {
                    switch (keyData)
                    {
                    case System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.F4:
                    {
                    return handleAltF4;
                    }
                    }
                    }
                    // process all others as normal
                    return false;
                    }

                    Dave

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    I finally got around to trying that, I doubt I'd ever need it, but it's good to have, thanks!

                    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