思路:
设置一个bool变量flag
在有改动的时候置为true
然后在Closing事件中根据flag的值作出动作

解决方案 »

  1.   

    this.Closing + new System.ComponentModel.CancelEventHandler(this.OnClosing);
    ...
    private void OnClosing(object sender,  System.ComponentModel.CancelEventArgs e)
    {
      if (MessageBox.Show("Are you sure", "Terminate the Application",
      MessageBoxButtons.YesNo) == DialogResult.No)
      e.Cancel = true; // default is false, assign to true to stop the termination process
    }
      

  2.   

    可以做一个全局bool变量
    可以在TextChange里面,如果改变量为false,则设置为true;
    在保存的方法里面,保存完之后设置为false;
    退出的时候判断,如果为true,就说明要保存,那就弹出一个MessageBox,点yes保存,no 退出
      

  3.   

    private bool flag = false;
    /// <summary>
    /// 文本变化事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
    if(!flag) flag = true;
    } /// <summary>
    /// 退出事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if(flag) MessageBox.Show("gaga");
    }
    /// <summary>
    /// 保存事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void button1_Click(object sender, System.EventArgs e)
    {
    flag = false;
    }
      

  4.   

    假设是多文档的:你 要关闭未保存的子窗口:
    在窗体的ONCLOSING事件添加如下代码:
    if(dirty)
      {
        if(MessageBox.Show("文件尚未保存.是否保存所做的修改?","提示",
          MessageBoxButtons.YesNo,MessageBoxIcon.Information)==DialogResult.Yes)
          menuItemSave_Click(参数列表);   }