WinForms - 表单关闭前的事件 [英] WinForms - Event before form is closing

查看:102
本文介绍了WinForms - 表单关闭前的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个winforms,我想在系统退出之前控制事件。请参阅下面的代码。也许你可以帮我一些想法:)





Hi all,

I have a winforms and i want to control the event before the system is exit. Please see my code below. Maybe you can help me with some idea :)


Private Sub frmDashboard_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim vAns As String = String.Empty
    Try
        vAns = MessageBox.Show("You will be logout from the system. Do you wish to continue?" & vbCrLf & _
                             "Click YES to logout." & vbCrLf & _
                             "Click NO to logout and terminate this system." & vbCrLf & _
                             "Click CANCEL to stay on system", "Confirmation!", MessageBoxButtons.YesNoCancel)
        If vAns = "0" Then
             ' Logout and back to login form
        ElseIf vAns = "1" Then
             ' Logout and terminate application
        ElseIf vAns = "2" Then
             ' Stay on current form
        End If
    Catch ex As Exception

    End Try
End Sub







如果用户点击是,或者没有系统将会关闭,无论如何。但是,如果我单击取消,它不应该做任何事情,但因为它在FormClosing事件下,系统将关闭,而不是保持当前的形式。怎么做?



想象一下,我有frmLogin和frmDashboard。这个动作是在frmDashboard







编辑:



我刚刚发现这个链接 [ ^ ]。它可以在这里回答我的问题吗?




By right if user click yes or no system will be closing no matter what. But if i click cancel, it should not do anything but since it is under FormClosing events, the system will close and not stay in the current form. How to do it?

Imagine i have frmLogin and frmDashboard. This action is on frmDashboard



EDITED:

I just found this link[^]. Can it answer my question here?

推荐答案

要取消关闭(即让表单停留,不关闭它),请为 e.Cancel 。



这些都清楚地写在文件中;阅读时你应该多加注意。



-SA
To cancel closing (that is, to let the form stay, not closing it), assign true to e.Cancel.

This is all clearly written in documentation; you should only pay more attentions while reading it.

—SA


这篇关于WinForms - 表单关闭前的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆