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. Disabling the 'X' button in the caption bar

Disabling the 'X' button in the caption bar

Scheduled Pinned Locked Moved C#
csharphelpquestion
6 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.
  • D Offline
    D Offline
    Dario Solera
    wrote on last edited by
    #1

    Hi, is there a way to disable the X (close) button in the title bar of a Form? I don't want to completely remove the controls from the title bar because I want to have the icon and the minimize button still visible. I tried a few ways based on the override of the WndProc method, but they didn't work, probably because they were in VB6 and porting them to C# broke something. Thanks for any help.

    _____________________________________________ Tozzi is right: Gaia is getting rid of us. My Blog [ITA] - Developing ScrewTurn Wiki 1.0 Beta3

    V R L 3 Replies Last reply
    0
    • D Dario Solera

      Hi, is there a way to disable the X (close) button in the title bar of a Form? I don't want to completely remove the controls from the title bar because I want to have the icon and the minimize button still visible. I tried a few ways based on the override of the WndProc method, but they didn't work, probably because they were in VB6 and porting them to C# broke something. Thanks for any help.

      _____________________________________________ Tozzi is right: Gaia is getting rid of us. My Blog [ITA] - Developing ScrewTurn Wiki 1.0 Beta3

      V Offline
      V Offline
      Vipin Venugopal
      wrote on last edited by
      #2

      I guess thats not possible. Even I wanted the same thing some time ago but failed to do it. i even tried with some java scripts there also i failed to get a result. So i guess it is not possible. Vipin

      1 Reply Last reply
      0
      • D Dario Solera

        Hi, is there a way to disable the X (close) button in the title bar of a Form? I don't want to completely remove the controls from the title bar because I want to have the icon and the minimize button still visible. I tried a few ways based on the override of the WndProc method, but they didn't work, probably because they were in VB6 and porting them to C# broke something. Thanks for any help.

        _____________________________________________ Tozzi is right: Gaia is getting rid of us. My Blog [ITA] - Developing ScrewTurn Wiki 1.0 Beta3

        R Offline
        R Offline
        Rahul RK
        wrote on last edited by
        #3

        Hello Friend, As i get your problem. Try this solution. Firstly Declair a bool IsClose = false; Then in, protected override void Dispose( bool disposing ) { if(IsClose) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } } Make IsClose = true when you want to close the form and then call Dispose(true); Rahul Kulkarni

        D 1 Reply Last reply
        0
        • R Rahul RK

          Hello Friend, As i get your problem. Try this solution. Firstly Declair a bool IsClose = false; Then in, protected override void Dispose( bool disposing ) { if(IsClose) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } } Make IsClose = true when you want to close the form and then call Dispose(true); Rahul Kulkarni

          D Offline
          D Offline
          Dario Solera
          wrote on last edited by
          #4

          I already use this method, but the close button is still enabled, so when the user clicks on it, nothing happens and it has not a good feeling. Thanks anyway.

          _____________________________________________ Tozzi is right: Gaia is getting rid of us. My Blog [ITA] - Developing ScrewTurn Wiki 1.0 Beta3

          1 Reply Last reply
          0
          • D Dario Solera

            Hi, is there a way to disable the X (close) button in the title bar of a Form? I don't want to completely remove the controls from the title bar because I want to have the icon and the minimize button still visible. I tried a few ways based on the override of the WndProc method, but they didn't work, probably because they were in VB6 and porting them to C# broke something. Thanks for any help.

            _____________________________________________ Tozzi is right: Gaia is getting rid of us. My Blog [ITA] - Developing ScrewTurn Wiki 1.0 Beta3

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            // constants for menu manipulation
            const int SC_CLOSE = 0xF060;
            const int MF_BYCOMMAND = 0x0;

            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern IntPtr RemoveMenu(IntPtr menuHwnd, int position, int flags );

            [System.Runtime.InteropServices.DllImport("user32.dll")]
            public static extern IntPtr GetSystemMenu(IntPtr hwnd, int bRevert);

            public static void EnableApplicationClose( System.Windows.Forms.Form form, bool val )
            {
            IntPtr menu = GetSystemMenu( form.Handle, val ? 1 : 0 );
            if ( !val && menu != IntPtr.Zero )
            RemoveMenu( menu, SC_CLOSE, MF_BYCOMMAND );
            else
            {
            // needed when enabling the app close box to force it to redraw.
            form.Enabled = false;
            form.Enabled = true;
            }
            }

            D 1 Reply Last reply
            0
            • L Lost User

              // constants for menu manipulation
              const int SC_CLOSE = 0xF060;
              const int MF_BYCOMMAND = 0x0;

              [System.Runtime.InteropServices.DllImport("user32.dll")]
              public static extern IntPtr RemoveMenu(IntPtr menuHwnd, int position, int flags );

              [System.Runtime.InteropServices.DllImport("user32.dll")]
              public static extern IntPtr GetSystemMenu(IntPtr hwnd, int bRevert);

              public static void EnableApplicationClose( System.Windows.Forms.Form form, bool val )
              {
              IntPtr menu = GetSystemMenu( form.Handle, val ? 1 : 0 );
              if ( !val && menu != IntPtr.Zero )
              RemoveMenu( menu, SC_CLOSE, MF_BYCOMMAND );
              else
              {
              // needed when enabling the app close box to force it to redraw.
              form.Enabled = false;
              form.Enabled = true;
              }
              }

              D Offline
              D Offline
              Dario Solera
              wrote on last edited by
              #6

              Thank you so much! It's exactly what I was looking for!

              _____________________________________________ Tozzi is right: Gaia is getting rid of us. My Blog [ITA] - Developing ScrewTurn Wiki 1.0 Beta3

              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