Form Close

jeva39

Well-known member
Joined
Jan 28, 2005
Messages
135
Location
Panama
Programming Experience
1-3
How I can to disable the X button (next to Maximize and Minimize form) to don´t permit the user close the form with this button and only permit close by a specific button I.E (Close, Exit, etc)

Thanks
 
if you want to still let the user maximize and minimize the form but not close the form in the form's closing event set e.Handled to True

VB.NET:
Private Sub Form1_Closing (...) Handles MyBase.Closing
  e.Handled = True
End Sub

this way the user can click the X button, but the form wont close
 
If I'm not mistaken, JuggaloBrotha's solution as is will prevent the Form closing by any method other than Application.Exit(), which does not raise the Closing event. If you have a Form level variable, set to False by default, which you set to True when you want to close the Form, you can test this variable in the Form.Closing event handler.
VB.NET:
Private canClose As Boolean = False

Private Sub CloseForm()
	Me.canClose = True
End Sub

Private Sub Form1_Closing(...) Handles MyBase.Closing
	e.Cancel = Not Me.canClose
End Sub
 
Back
Top