2012-01-11 37 views
0

在vb.net Windows應用程序中,我需要用戶在關閉應用程序之前進行確認。我在FormClosing情況下,本代碼通過用戶選擇取消表單關閉

If BackgroundWorker1.IsBusy Then 
    Dim UserSelection As Integer = MsgBox("Do you want Cancel Processing and Exit Application?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Exit Application") 
    If UserSelection = 6 Then 
     BackgroundWorker1.CancelAsync() 
     e.Cancel = True 
    Else 
     ???? 
    End If 
End If 

我怎樣才能取消形式結束,如果用戶點擊No

嘗試e.Cancel = false但它沒有工作(退出應用程序)。

+2

'e.Cancel = True'會停止表單關閉。 – keyboardP 2012-01-11 04:58:54

+0

@keyboardP謝謝..我錯誤地把其他的方式,對我的愚蠢...抱歉... :) – Nalaka526 2012-01-11 05:01:01

+0

不客氣! – keyboardP 2012-01-11 05:01:29

回答

3

e.Cancel = True將停止形式收盤防止形式。

1

據文件「e.Cancel =真」,從關閉

0

這是關閉取消表單的完整代碼。我們應該使用FormClosing事件。

Private Sub Frm1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
     If MessageBox.Show("Do you want to closed", Me.Text, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then 
      e.Cancel = True 
     End If 
    End Sub