Reputation: 3235
I can not believe I am asking this question after reading similar questions mostly OLD and here is the frustration I had code that worked just as desired then I changed to Option Strict ON now more issues than I bargained for
Here is the code that works OR I should say worked at one time
In fact it still works on another test project?
The Handles Me.Closing has a RED ERROR squiggle line under it
Private Sub frmTwo_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO Back to Form Start"
Const caption As String = "Exit OR Cancel"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
Application.Exit()
ElseIf result = DialogResult.No Then
frmStart.Show()
Me.Hide()
'tbMessage.Text = "CANCEL"
e.Cancel = True
End If
End Sub
Tested this code on two forms frmStart and frmTwo on frmTwo it does not stop Debugging
Private Sub frmStart_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
e.Cancel = False
Else
Me.Hide()
frmTwo.Show()
e.Cancel = True
End If
End Sub
Upvotes: 0
Views: 147
Reputation: 1371
It seems with Option Strict ON VB.Net does not like constructing the Sub this way
Private Sub frmStart_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
And as mentioned you then need to include Imports System.ComponentModel
So to STOP playing e.Cancel = True and e.Cancel = False just write a Sub as seen below and Call that Sub from some Button no need for Imports System.ComponentModel
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
CloseForm()
End Sub
Public Sub CloseForm()
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO Return to Form Start"
Const caption As String = "Exit OR Return"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
Application.Exit()
ElseIf result = DialogResult.No Then
Close()
frmStart.Show()
End If
End Sub
Upvotes: 2
Reputation: 562
Recreating the closing event, the event parameters are (sender As Object, e As CancelEventArgs)
not the different types. I'm testing this against .Net Framework 4.8, what .Net are you targeting?
This worked as expected:
Private Sub frmStart_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
Const message As String = "YES Exit Program" + vbCrLf + vbNewLine + "NO CANCEL"
Const caption As String = "Exit OR Cancel"
Dim result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.No Then
Me.Text = "CANCEL"
e.Cancel = True
End If
End Sub
Upvotes: 2