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

前端之家收集整理的这篇文章主要介绍了VB.Net中确认退出对话框的实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在敲代码的时候,为了更加人性化的设计,我们在用户退出系统之前需要进行提示,如下图:

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

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

a.

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


b.

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


c.

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

d.(最为简单)

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


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

二、Button_Click事件

  1. Private Sub btnQuit_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnQuit.Click
  2.  
  3. If MsgBox("是否要退出系统?",MsgBoxStyle.YesNo + MsgBoxStyle.Question,"提示") = MsgBoxResult.Yes Then
  4. Application.Exit()
  5. End If
  6.  
  7. End Sub

猜你在找的VB相关文章