Disable X (close) button in Control box

Using the form's Closing event and e.Cancel
Visual Basic:
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

  If blah Then
      e.Cancel = True 'keeps form from closing
  Else : e.Cancel = False 'closes the form
  End If
End Sub
 
Nope, but you can set the ControlBox properity to False and remove it and the Maximize and Minimize buttons. Then make your own button for closing the form.
 
This is how you do it in VB6... should work on .net too but I never tryed it!

Visual Basic:
Option Explicit    
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long,ByVal bRevert As Long) As Long    

Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long,ByVal nPosition As Long, ByVal wFlags As Long) As Long         

Private Const MF_BYPOSITION = &H400&




Public Function DisableCloseButton(frm As Form) As Boolean

'PURPOSE: Removes X button from a form
'EXAMPLE: DisableCloseButton Me
'RETURNS: True if successful, false otherwise
'NOTES:   Also removes Exit Item from
'         Control Box Menu


    Dim lHndSysMenu As Long
    Dim lAns1 As Long, lAns2 As Long
    
    
    lHndSysMenu = GetSystemMenu(frm.hwnd, 0)

    'remove close button
    lAns1 = RemoveMenu(lHndSysMenu, 6, MF_BYPOSITION)

   'Remove seperator bar
    lAns2 = RemoveMenu(lHndSysMenu, 5, MF_BYPOSITION)
    
    'Return True if both calls were successful
    DisableCloseButton = (lAns1 <> 0 And lAns2 <> 0)

End Function


Alex :p
 
Back
Top