Disable close button

Can we disable the close button on a form I can remove the minimize/maximize, but cant find anyway to disable the close button.


Answer this question

Disable close button

  • David Teitlebaum MSFT

    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


     

     


  • tmcalees



    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


  • Tan Silliksaar

    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.


  • Christatos

    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


  • Steven Randolph

    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.
  • damuser

    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


  • Yogesh Ranade

    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


  • ManchesterMike

    Yes, use my CreateParams method.
  • Disable close button