我想知道我可以在下面第一个if语句的第二部分输入什么代码,以便在单击“取消”按钮时保持表单处于活动状态。
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
'Message prompt to ask if the user really want to exit
MsgBox("Are you sure you want to exit?", MsgBoxStyle.OkCancel, "MDCS")
If MsgBoxResult.Ok Then
If Application.OpenForms().OfType(Of Find_Client).Any Or Application.OpenForms().OfType(Of Form_items).Any Then
MsgBox("Close all opened windows before the main", MsgBoxStyle.OkOnly, "MDCS")
Else
' Call sub that close the program
Me.Close()
Dim items As New Form_items, clients As New Find_Client
items.Close()
clients.Close()
'Stop Stopwatch
Timer1.Stop()
Me.StopWatch.Stop()
'Mark Stopwatch value
intTimerValues = Val(Timer_Label.Text)
End If
Else
???
End If
End Sub
答案 0 :(得分:0)
通常情况下,当前活动表单应自动显示 ,但如果您将代码放在特定的按钮事件处理程序中,则不会在Else函数中放置任何代码。 在上面的评论中阅读你的回复后,我认为你将代码放在 Form_Closing 事件中,如果是这样,那么你应该把 e.Cancel() 在代码的其他部分。
答案 1 :(得分:0)
您拥有的代码不正确。
您会显示一个消息框
'Message prompt to ask if the user really want to exit
MsgBox("Are you sure you want to exit?", MsgBoxStyle.OkCancel, "MDCS")
但你没有测试它的返回值。
实际所做的是测试常量的值,MsgBoxResult.Ok
If MsgBoxResult.Ok Then
MsgBoxResult.Ok
is a member of an enumeration评估为1.基本上,你所拥有的是
If 1 Then
好吧,总是评估为True
,因此始终会运行If
块。你永远不会进入Else
区块。
通过实际测试MsgBox
调用的结果来修复代码:
Dim MsgBoxResult As result = MsgBox("Are you sure you want to exit?", _
MsgBoxStyle.OkCancel, _
"MDCS")
If result = MsgBoxResult.Ok Then
' ...
'Close the form.
Me.Close()
Else
' ...
' Don't write code to close the form and it won't be closed.
End If