Winform关闭窗口时弹出确认窗口的实现

下面基于常用的两种关闭方式:

1. 点击窗口的关闭按钮来关闭form

2. 点击一个button(或其他控件)来关闭form

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (MessageBox.Show("Exit?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
    {
        this.Dispose();
        Application.Exit();
    }
    else
    {
        e.Cancel = true;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    if (MessageBox.Show("Exit?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
    {
        this.Dispose();
        Application.Exit();               
    }
}


你可能感兴趣的:(object,button,WinForm)