现在的位置: 首页 > 综合 > 正文

在关闭窗体时弹出确认对话框

2012年08月21日 ⁄ 综合 ⁄ 共 1667字 ⁄ 字号 评论关闭

Frm_Main.cs

View Code

 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace ShowDialogByClose
10 {
11 public partial class Frm_Main : Form
12 {
13 public Frm_Main()
14 {
15 InitializeComponent();
16 }
17
18 private void Frm_Main_FormClosing(object sender, FormClosingEventArgs e)//触发窗体关闭事件
19 {
20 if (MessageBox.Show("将要关闭窗体,是否继续?", "询问", MessageBoxButtons.YesNo) == DialogResult.Yes)//判断是否单击了“是”按钮
21 {
22 e.Cancel = false;//关闭窗体
23 }
24 else
25 {
26 e.Cancel = true;//取消事件的执行
27 }
28 }
29 }
30 }

Frm_Main.designer.cs

View Code

 1 namespace ShowDialogByClose
2 {
3 partial class Frm_Main
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows 窗体设计器生成的代码
24
25 /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.SuspendLayout();
32 //
33 // Frm_Main
34 //
35 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
36 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
37 this.BackgroundImage = global::ShowDialogByClose.Properties.Resources._04;
38 this.ClientSize = new System.Drawing.Size(289, 136);
39 this.Name = "Frm_Main";
40 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
41 this.Text = "在关闭窗体时弹出确认对话框";
42 this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Frm_Main_FormClosing);
43 this.ResumeLayout(false);
44
45 }
46
47 #endregion
48 }
49 }

抱歉!评论已关闭.