我想在单击这个关闭按钮的时候,调用一个函数,判断功能完成与否,若否,则不能关闭此窗体,若是,则关闭释放此窗体。
请高手指教~

解决方案 »

  1.   

    form1.Closing += new CancelEventHandler(Form1_Closing);private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
       // Determine if text has changed in the textbox by comparing to original text.
       if (textBox1.Text != strMyOriginalText)
       {
          // Display a MsgBox asking the user to save changes or abort.
          if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
             MessageBoxButtons.YesNo) ==  DialogResult.Yes)
          {
             // Cancel the Closing event from closing the form.
             e.Cancel = true;
             // Call method to save file...
          }
       }
    }
      

  2.   

    或者直接重写受保护的 OnClosing() 方法:private override OnClosing(System.ComponentModel.CancelEventArgs e)
    {
       base.OnClosing();
       // Determine if text has changed in the textbox by comparing to original text.
       if (textBox1.Text != strMyOriginalText)
       {
          // Display a MsgBox asking the user to save changes or abort.
          if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
             MessageBoxButtons.YesNo) ==  DialogResult.Yes)
          {
             // Cancel the Closing event from closing the form.
             e.Cancel = true;
             // Call method to save file...
          }
       }
    }
      

  3.   

    protected override OnClosing(System.ComponentModel.CancelEventArgs e)
    {
      base.OnClosing();
      if (/*不想关闭窗体*/) e.Cancel = true;
    }
      

  4.   

    楼上的兄弟真是快,我刚发出closing时候,就看到你的帖子了,不过还是学到e.cancel了,呵呵,谢谢~!