【问题标题】:VB.NET: Abort FormClosing()VB.NET:中止 FormClosing()
【发布时间】:2011-01-16 10:40:40
【问题描述】:

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

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

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

【问题讨论】:

    标签: vb.net formclosing


    【解决方案1】:

    你可以试试

    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 Property

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

    【讨论】:

    【解决方案2】:

    '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
    

    【讨论】:

      猜你喜欢
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多