9

我有一个要在应用程序关闭时运行的代码片段。所以,我使用FormCLosing了事件。但现在我想放置一条退出确认消息。就像,如果用户点击 Exit( X) 按钮,会有一个提示,如果他点击 NO,那么应用程序将不会关闭并恢复到之前的状态。

FormClosing现在我发现使用事件很难实现。因为无论用户点击什么按钮,它都会被执行。有什么补救措施吗?

我的意思是,我需要一个甚至喜欢ExitButtonPressed()..

4

2 回答 2

22

你可以尝试类似的东西

Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    If (MessageBox.Show("Close?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No) Then
        e.Cancel = True
    End If
End Sub

看一下

FormClosingEventArgs 类

CancelEventArgs.Cancel 属性

可以通过将 Cancel 属性设置为 true 来取消该事件。

于 2010-02-13T07:51:08.777 回答
0

' 表单的 Button2 和 closebutton 都关闭了询问相同 ' 问题的表单

Dim button2Yes As Boolean = False Private Sub Button2_Click(sender As Object, e As EventArgs) 处理 Button2.Click

    If MessageBox.Show("    Sure to close?   ", "CLOSING CONTROL", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        button2Yes = True
        Me.Close()
    Else
        button2Yes = False
    End If
End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.FormClosing
    If Not button2Yes Then
        If Not MessageBox.Show("    Sure to close?   ", "CLOSING CONTROL", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            e.Cancel = True
        End If
    End If
End Sub
于 2017-11-19T18:48:11.670 回答