VB.Net中确认退出对话框的实现

实现方法分为两大类:窗体事件和控件事件,下面就一一展示:

    一、FormClosing事件(又分以下几种方法)

    a.

Private Sub frmPractise_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MessageBox.Show("确定退出吗?", "退出确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            e.Cancel = True
        End If
    End Sub

    b.

Private Sub Practise_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MsgBox("确定退出吗?", MessageBoxButtons.OKCancel, "退出确认") = Windows.Forms.DialogResult.Cancel Then
            e.Cancel = True
        End If
    End Sub

    c.

Private Sub Practise_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim p As Integer
        p = MsgBox("你真的要退出系统吗?", MsgBoxStyle.OkCancel, "提示")
        If p = 2 Then
            e.Cancel = True
        End If
    End Sub

    d.(最为简单)

Private Sub Practise_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MsgBox("你确认要退出程序吗?", MsgBoxStyle.OkCancel, "退出提示") = MsgBoxResult.Cancel Then e.Cancel = True
    End Sub

    以上只是点击窗口关闭按钮时的用法,若直接在窗体控件上点击退出时,可以用以下方式实现:

    二、Button_Click事件

Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click

        If MsgBox("是否要退出系统?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "提示") = MsgBoxResult.Yes Then
            Application.Exit()
        End If

    End Sub

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。