none
Disable close button

    Question

  • Can we disable the close button on a form ? I can remove the minimize/maximize, but cant find anyway to disable the close button.
    Wednesday, June 08, 2005 7:32 PM

Answers

  • Not sure if you can make it go away, but you can disable the close using the FormClosing event.

    This code allows the form to be closed by clicking a button on the form, but not by clicking the form's close button.



    Private okToClose As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        okToClose = True

        Me.Close()

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If Not okToClose Then

            e.Cancel = True

        End If

    End Sub

     



    Robert

    Thursday, June 09, 2005 3:39 PM
  • Here's an alernate method:

    Protected Overrides ReadOnly Property CreateParams() As CreateParams

    Get

    Dim cp As CreateParams = MyBase.CreateParams

    Const CS_NOCLOSE As Integer = &H200

    cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE

    Return cp

    End Get

    End Property


     

     

    Tuesday, June 14, 2005 4:04 PM

All replies



  • private sub thanks()
    {
        Dim frm as new Form2
        frm.MinimizeBox = False
        frm.MaximizeBox = False
        frm.ControlBox = False
        frm.Show()
    }

     


    this code would make all that go away in this example i was just creating a new form off a button but you could put this in the load event of your form and it would launch that way
    Wednesday, June 08, 2005 11:41 PM
  • Thanks for reply, but thats not what I want, if i set the controlbox=false, all maximize, minimize, close and the form icon will be gone, I just the close to be gone or disabled.
    Thursday, June 09, 2005 3:23 PM
  • well after doing a little research I have found the answer... here is the code in a CloseButton Class that should do the trick



    Option Explicit On
    Option Strict
    On

    Public

    Class CloseButton   Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal revert As Integer) As Integer
      Private Declare Function EnableMenuItem Lib "user32" (ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As Integer
      Private Const SC_CLOSE As Integer = &HF060
      Private Const MF_BYCOMMAND As Integer
    = &H0
      Private Const MF_GRAYED As Integer
    = &H1
      Private Const MF_ENABLED As Integer
    = &H0
      Private Sub New()
      End Sub
      Public Shared Sub Disable(ByVal form As System.Windows.Forms.Form)
        ' The return value specifies the previous state of the menu item (it is either
        ' MF_ENABLED or MF_GRAYED). 0xFFFFFFFF indicates   that the menu item does not exist.
        Select Case EnableMenuItem(GetSystemMenu(form.Handle.ToInt32, 0), SC_CLOSE, MF_BYCOMMAND Or
    MF_GRAYED)
          Case
    MF_ENABLED
          Case
    MF_GRAYED
          Case
    &HFFFFFFFF
            Throw New
    Exception("The Close menu item does not exist.")
          Case
    Else
        End
    Select
      End
    Sub

    End

    Class


     



    then just call this on a form load or whatever... it worked for me when i tested it
    Thursday, June 09, 2005 3:39 PM
  • Not sure if you can make it go away, but you can disable the close using the FormClosing event.

    This code allows the form to be closed by clicking a button on the form, but not by clicking the form's close button.



    Private okToClose As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        okToClose = True

        Me.Close()

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If Not okToClose Then

            e.Cancel = True

        End If

    End Sub

     



    Robert

    Thursday, June 09, 2005 3:39 PM
  • Here's an alernate method:

    Protected Overrides ReadOnly Property CreateParams() As CreateParams

    Get

    Dim cp As CreateParams = MyBase.CreateParams

    Const CS_NOCLOSE As Integer = &H200

    cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE

    Return cp

    End Get

    End Property


     

     

    Tuesday, June 14, 2005 4:04 PM
  • Hi

    I think that the following is what you are wanting, try it please!

    Create a new project and insert a new class that must have the following

    Public Class close_button

    Private Declare Function EnableMenuItem Lib "user32" (ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As Integer

    Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Int32, ByVal bRevert As Boolean) As Int32

    Private Const SC_CLOSE As Integer = &HF060

    Private Const MF_BYCOMMAND As Integer = &H0

    Private Const MF_GRAYED As Integer = &H1

    Private Const MF_ENABLED As Integer = &H0

    Public Sub enable(ByVal form As Form)

    EnableMenuItem(GetSystemMenu(form.Handle, False), SC_CLOSE, MF_ENABLED)

    End Sub

    Public Sub disable(ByVal form As Form)

    EnableMenuItem(GetSystemMenu(form.Handle, False), SC_CLOSE, MF_GRAYED)

    End Sub

    End Class

    then you are ready to use, on the form1 create 2 buttons and change do text to "enable" and "disable" respectivly, the code must be the following:

    Public Class Form1

    Dim close_button As New close_button

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    close_button.enable(Me)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    close_button.disable(Me)

    End Sub

    End Class

    if you want to use with another form put for example Form2.Handle intead of Me

    Tuesday, February 28, 2006 9:10 PM
  • Hello,

    I have this working fine. I put the close_button.enable(Me) method in my load event. However, when I minimized the form, then maximized it, my close button went back to enabled. Is there a workaround for this?

    Thanks.

    Tuesday, October 31, 2006 8:59 PM
  • Yes, use my CreateParams method.
    Tuesday, October 31, 2006 10:18 PM
  • i got the same problem..but can any provide the code in c# ?...and one more thing, my problem is when i click on close button..my application is not coming to exit.still it's running.that's why i put one button and write the code as application.exit.....when i click close in other windows application is coming to exit..i dont know what's the problem in this form..here i have to disabule the close button, or close button has to work..

    thanks in advance...
    Wednesday, February 10, 2010 11:17 AM
  • private Boolean oktoclose=false;
            private void BtnExit_Click(object sender, EventArgs e)
            {
                oktoclose = true;
                Application.Exit();
            }
            private void UsersForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (oktoclose==false)
                    e.Cancel=true;
       
            }

    this code worked fine...thanks
    Wednesday, February 10, 2010 11:35 AM
  • Hmm asking for C# code on a VB forum from a 5 year old topic I don't know where to begin telling you what's wrong with that .
    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Wednesday, February 10, 2010 11:43 AM