c# 点击关闭窗体按钮事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dreamdonghui/article/details/84842768

背景

很多时候需要在程序退出之前做一些处理。比如提醒用户是否要保存更改的数据。

实现

主要两点:
1.注册退出事件及要触发的函数。
2.写要触发的函数。
所需要的相关函数Form.Closing Event.
#代码示例
今天正好要新写一个程序,正好把此部分贴出来:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ROVControl
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //点击关闭按钮时触发此函数。
        //This event happened when the user closing the main form. 
        private void MainForm_Closing(object sender, CancelEventArgs e)
        {
            MessageBox.Show("This is the first thing I want know!");
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //regist the form closing event.
            //注册窗体关闭事件。
            this.FormClosing += new FormClosingEventHandler(MainForm_Closing);
        }
    }
}


猜你喜欢

转载自blog.csdn.net/dreamdonghui/article/details/84842768