2014-02-13 129 views
0

這是我的代碼:Windows窗體不關閉

Private Sub frmName_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    If e.CloseReason = CloseReason.UserClosing Then 
     If MessageBox.Show("Are you sure you want to logout?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then     
      Me.Close() 
     Else 
      e.Cancel = True 
     End If 
    End If 
End Sub 

如果我點擊no將取消的形式結束,但是當我點擊yes會出現反覆的消息框。我想要發生的是當我點擊關閉按鈕,並點擊yes它將關閉窗體。我怎樣才能解決這個問題?

+0

具有諷刺意味的是,如果'MessageBox.Show'沒有阻止你會遇到一個'* StackOverflow的* Exception' ... Markus'es答案是正確的,我都無所謂。 Close'調用FormClosing事件,以便刪除該調用。 – MrPaulch

回答

1

由於表格已經關閉,因此不需要再次撥打Close()。下面的代碼應該工作:

Private Sub frmName_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    If e.CloseReason = CloseReason.UserClosing Then 
     If MessageBox.Show("Are you sure you want to logout?", _ 
       "Exit", MessageBoxButtons.YesNo, _ 
       MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then     
      e.Cancel = True 
     End If 
    End If 
End Sub 
+0

很快。謝謝! –

+0

@AdrianEnriquez:不客氣 – Markus