2014-02-09 51 views
1

我創建了一個消息框,詢問用戶他/她是否想關閉應用程序。Visual Basic MsgBox退出

我想出了這個類:

Private Sub closeAll_Click(sender As Object, e As EventArgs) Handles closeAll.Click 
     MsgBox("Do you want to terminate the program?", MsgBoxStyle.YesNo, "Close?") 
     If MsgBoxResult.Yes Then 
      Application.Exit() 
     End If 
    End Sub 

如果我選擇「是」,它工作得很好,因爲它成功地退出應用程序。但是如果選擇「否」,它仍然會關閉。它是否真的需要一個「其他」語句來做到這一點?

如果是這樣,我不知道讓程序「不做什麼」的正確編碼。

有人可以幫忙嗎?

+0

如果MsgBox(..)= MsgBoxResult.Yes Then –

回答

1

MsgBox()是返回結果(MsgBoxResult枚舉)功能讓你的代碼應該是:

Private Sub closeAll_Click(sender As Object, e As EventArgs) Handles closeAll.Click 
    If MsgBox("Do you want to terminate the program?", MsgBoxStyle.YesNo, "Close?") = MsgBoxResult.Yes Then 
     Application.Exit() 
    End If 
End Sub 

你所寫的方法是你把枚舉MsgBoxResult.Yes的價值,並檢查它是否是真的。這會導致隱式轉換爲布爾值,因爲枚舉值不爲零,所以這是真的。