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 Form2 : Form { public Form2() { InitializeComponent(); } bool modified; private void button1_Click(object sender, EventArgs e) { //save data modified = true;
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 Form2 : Form
{
public Form2()
{
InitializeComponent();
}
bool modified;
private void button1_Click(object sender, EventArgs e)
{
//save data
modified = true;
} private void Form2_Load(object sender, EventArgs e)
{
modified = false;
this.FormClosing+=new FormClosingEventHandler(Form2_FormClosing);
} void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (modified == false)
{
MessageBox.Show("数据没有保存,请点击保存按钮!");
e.Cancel=true;
}
} }
}
将Form2 窗体以ShowDialog的方式显示。
{ //遍历窗体控件,判断数据是否一致,设置全局标识
if (MessageBox.Show(this, "是否退出系统?", "系统", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.No)
{
e.Cancel = true;
}
}
{
Form2 form2 = new Form2();
form2.ShowDialog();
form2.Dispose();
}
(2)在TextBox之类的编辑控件的valueChaned事件中设置已修改状态
(3)在保存事件完成之后,初始化 已修改状态
(4)窗口关闭事件中,判断是否保存,没保存时,忽略关闭事件。