我想从" Sub"关闭主表单;因为我正在使用"退出"菜单也是; 我在下面编写了代码,但是当用户点击"取消"在YesNoCancel对话框中; 我的代码如下:
If MekdamFirstLetters = "*" Then
Dim result = MessageBox.Show("The File: " & _
vbCrLf & _
" (" & File & ".txt) has been changed," & _
vbCrLf & _
" do you want to save it? ", _
"Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If result = DialogResult.Cancel Then
'***********************
' THE ISSUE IS HERE;
'Me.Cancel = True
'***********************
ElseIf result = DialogResult.No Then
Application.Exit()
ElseIf result = DialogResult.Yes Then
SaveFileDialog()
Application.Exit()
End If
End If
答案 0 :(得分:3)
正如我在评论中提到的,您最好的选择是将您的代码放入FormClosing
EventHandler中,这样您就有机会取消关闭。
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If MekdamFirstLetters = "*" Then
Dim result = MessageBox.Show("The File: " & _
vbCrLf & _
" (" &
File & ".txt) has been changed," & _
vbCrLf & _
" do you want to save it? ", _
"Mekdam Message 701", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
If result = DialogResult.Cancel Then
e.Cancel = True 'This will stop the form from closing
ElseIf result = DialogResult.No Then
'Application.Exit() 'no need to explicitly exit, you are already doing it this will lead to a loop
ElseIf result = DialogResult.Yes Then
SaveFileDialog()
'Application.Exit() 'Same here
End If
End If
End Sub