VB.NET:中止 FormClosing()

发布于 2024-08-21 08:26:37 字数 283 浏览 6 评论 0原文

我有一个代码片段,我想在应用程序关闭时运行。因此,我使用了 FormCLosing 事件。但现在我想放置一条退出确认消息。例如,如果用户单击“退出”(X) 按钮,则会出现提示,如果单击“否”,则应用程序不会关闭并恢复到之前的状态。

现在我发现使用 FormClosing 事件很难实现。因为无论用户单击哪个按钮它都会被执行。 有什么补救办法吗?

我的意思是,我需要一个像 ExitButtonPressed()..

I have a code snippet that I want to run when the app is closing. So, I used FormCLosing event. But now i wanna place a confirmation message for exiting. Like, if the user clicks the Exit(X) button, there'll be a prompt, if he clicks NO, then the app will not close and revert to previous state.

Now I find that hard to achieve using FormClosing event. because it'll get executed no matter what button the user clicks.
Any remedy for that?

I mean, I need an even like ExitButtonPressed()..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水溶 2024-08-28 08:26:37

您可以尝试

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 Class

CancelEventArgs.Cancel 属性

可以通过设置取消活动
将 Cancel 属性设置为 true。

You could try something like

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

Have a look at

FormClosingEventArgs Class

And

CancelEventArgs.Cancel Property

The event can be canceled by setting
the Cancel property to true.

烟雨凡馨 2024-08-28 08:26:37

'表单的 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

'Button2 and closebutton of the form both closes the form asking the same 'question

Dim button2Yes As Boolean = False
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文