Disable the form close button (X)

flynn

Regular
Joined
Jul 28, 2005
Messages
59
I am using this code to disable the "X" (close) control on a .Net 2.0 form, but is there a better way? I'd rather use code from the .Net framework instead of VB6 code.

Code:
Public Class Form1
    Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
    Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer

    Public Const SC_CLOSE = &HF060&
    Public Const MF_BYCOMMAND = &H0&


    Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Dim SysMenuHandle As Integer
        SysMenuHandle = GetSystemMenu(Me.Handle().ToInt32(), False)
        RemoveMenu(SysMenuHandle, SC_CLOSE, MF_BYCOMMAND)
    End Sub

End Class

tia,
flynn
 
Visual Basic:
  Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        e.Cancel = True
    End Sub

Obviously that only stops it working, doesn't stop it showing up.
 
The method that you are using is actually Windows API, not necessarily VB6, and this feature is not present in the .Net Framework as far as I know, unless you want to set the ControlBox property to false and lose the Maximize, Minimize, and Close buttons as well as the icon.
 
Back
Top