---
title: "C#监听事件（窗体关闭、按键等）"
author: "ruohanfly"
date: "2016-01-13T15:18:07Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2016-01-13T15:18:07Z"
id: 2270317
url: "https://ruohanfly.iteye.com/blog/2270317"
---

1.监听窗体关闭事件（rslr\_main为窗体名）

```
        public rslr_main()
        {
            InitializeComponent();
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form_Closing);
        }
        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            ExcelExit();
            Owner.Show();
        }
```

 2.按键监听

```
        //下一件快捷键
        private void rssm_main_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.D) //"Ctrl + D"触发
            {
                //MessageBox.Show("快捷键");
           }
        }
```

 要把窗体的KeyPreview设置为true才有效

还要在\*\*.Designer.cs里边加入

```
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.rssm_main_KeyDown);
```

 这样就可以监听到了