Question How do I code for the exit cross in the top right corner?

BillO

Member
Joined
Sep 20, 2010
Messages
9
Programming Experience
Beginner
Hi there,

I have managed to create and code a file menu exit with the following code, however I just can't seem to find out how I am able to edit the red X in the top right hand side of the form next to the minimise and maximise buttons?

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Dim close As String
close = MessageBox.Show("Are you sure you want to exit?", "Exit Log Book", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If close = 6 Then
Me.Close()
Application.Exit()
Else
txtOdoStart.Focus()
End If
End Sub

Many thanks,
Bill
 
In your Exit menu item, simply use Me.Close(), then in the form's FormClosing event:
VB.NET:
e.Cancel = (MessageBox.Show("Are you sure you want to exit?", "Exit Log Book", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No)
 
Exit stage right and left

:D Many Thanks JuggaloBrotha!

After some playing around I basically get what you meant. Initially I had to close the menu twice before I got it. The big tip was the Form_Closing event. For some reason it didn't like me using dialog.result? So I ended up using:

Private Sub frmLogBook_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim close As String
close = MessageBox.Show("Are you sure you want to exit?", "Exit Log Book", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If close = 7 Then
e.Cancel = True
Else
e.Cancel = False
End If
End Sub

I know technically I'm not supposed to do it this way due to some string/double conversion issue when strict options are on. Nonetheless, everything is working perfectly even if the code could be more efficient/correct!
 
If you look up the MessageBox.Show you'll see that the function returns a DialogResult (System.Windows.Forms.DialogResult) so rather than first saving it to a string & then comparing it to an integer, you really should just compare the DialogResult to a DialogResult.

Also, at the top of that form's code put this line:
Option Strict On
 

Latest posts

Back
Top