2013-06-26 45 views
2

我有,當我按下關閉按鈕basicaly說:「你確定要退出」,但是當我點擊任何按鈕或取消,但程序關閉任何如何彈出一個消息框確認程序關閉與一個消息

這是我的代碼:

'Close Button 
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click 

    Dim result = MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel) 
    Me.Close() 

End Sub 
+1

Forgivness?你什麼意思 ? – Raptor

+1

你從來沒有測試的結果 –

+0

可能重複[VB.NET:MessageBox的與YesNoCancel - 否取消觸發相同的事件](http://stackoverflow.com/questions/2256909/vb-net-messagebox-with-yesnocancel-no -cancel-觸發器-相同事件) – billinkc

回答

3

你沒有做什麼與result的值。您需要檢查該值並確定是否調用Me.Close()。代碼大約

If result = DialogResult.Yes Then 
    Me.Close() 
End If 
2

發出Me.Close()無論result是什麼。檢查結果並執行Me.Close()只有用戶點擊Yes

2
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click 
    If MsgBox("Are you sure you want to quit?", MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2, "Close application") = Windows.Forms.DialogResult.Yes Then 
    Me.Close() 
    End If 
End Sub 
3

如果使用則消息框,以防止意外的形式接近,你的做法可能並不總是工作。如果用戶以其他方式關閉應用程序而不是單擊「關閉」按鈕,消息框將不會顯示。

嘗試使用FormClosing事件。

'Close Button 
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click 
    Me.Close() 
End Sub 

'FormClosing Event 
Private Sub MyForm_Closing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    If MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel) <> DialogResult.Yes 
     e.Cancel = True 
    End If 
End Sub 
1
Dim result = MessageBox.Show(" Are you sure you want to quit", "System Reminder", MessageBoxButtons.YesNo) 
    If result = DialogResult.Yes Then 
     Me.Close() 

    End If 
2

拷貝:

Dim result = MessageBox.Show(" Are you sure you want to end the Application", "School Management System", MessageBoxButtons.YesNoCancel) 
    If result = DialogResult.Yes Then 
     Me.Close() 
    End If