如何做到在点击一个Winform的关闭按钮时,不是直接退出程序,而是弹出一个对话框提示用户是否退出呢?只要添加Form的FormClosing事件就可以了。
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void OnClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("确实要退出吗", "hello", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
e.Cancel = true;
}
}
}