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
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
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
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,328
Location
Sydney, Australia
Programming Experience
10+
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
 
Top Bottom