You would paste this into your form's code to use it. This however permanently disables the close button. Doing it dynamically requires very different code, you have to modify the system menu. Paste this code into your form and use the CloseEnabled property in your logic:
Public Property CloseEnabled() As Boolean
Get
Return mCloseEnabled
End Get
Set(ByVal value As Boolean)
If value <> mCloseEnabled Then
mCloseEnabled = value
setSystemMenu()
End If
End Set
End Property
Private mCloseEnabled As Boolean = True
Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
MyBase.OnHandleCreated(e)
setSystemMenu()
End Sub
Private Sub setSystemMenu()
Dim menu As IntPtr = GetSystemMenu(Me.Handle, False)
Dim enable As Integer
If Not mCloseEnabled Then enable = 1
EnableMenuItem(menu, SC_CLOSE, enable)
End Sub
'' P/Invoke declarations
Private const SC_CLOSE As Integer = &hf060
Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal revert As Boolean) As IntPtr
Private Declare Function EnableMenuItem Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal IDEnableItem As Integer, ByVal wEnable As Integer) As Integer