【问题标题】:Handling Cancel Button in Yes/No/Cancel Messagebox in FormClosing Method在 FormClosing 方法中处理是/否/取消消息框中的取消按钮
【发布时间】:2009-08-09 23:37:47
【问题描述】:

我在表单的 FormClosing 方法中放置了一个Yes/No/Cancel Messagebox。现在这是消息框文本:您要保存数据吗?

我不是专业人士,不知道用户点击取消按钮怎么办? 点击取消按钮的确切结果必须是表单保持打开状态。
如何防止在FormClosing方法中关闭我的表单?

到目前为止我写的:;)

DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);

//...
else if (dr == DialogResult.Cancel)
{
    ???
}

请帮我完成我的代码!
谢谢

【问题讨论】:

    标签: c# .net messagebox


    【解决方案1】:

    FormClosing 有一个布尔参数,如果在函数返回时设置为 True,将取消关闭表单 IIRC。

    编辑:例如,

    private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
        // Set e.Cancel to Boolean true to cancel closing the form
    }
    

    See here.

    【讨论】:

      【解决方案2】:

      实际上,我认为您缺少事件处理程序,哦,即使没有偶数处理程序,您也无法转向。您必须使用这样的事件处理程序添加事件。

      private void myform_Closing(object sender, FormClosingEventArgs e) 
      {
          DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning)
      
          if (dr == DialogResult.Cancel) 
          {
              e.Cancel = true;
              return;
          }
          else if (dr == DialogResult.Yes)
          {
              //TODO: Save
          }
      }
      
      //now add a default constructor 
      public myform()  // here use your form name.
      {
          this.FormClosing += new FormClosingEventHandler(myform_Closing); 
      }
      

      如果这段代码有拼写错误,请见谅,因为我不是用c#写的,在这里复制粘贴。我只是在这里写的。 :)

      【讨论】:

        【解决方案3】:

        你可以有类似下面的东西:

        if(dr == DialogResult.Cancel)
        {
            e.Cancel = true;
        }
        else if(dr == DialogResult.Yes)
        {
            //Save the data
        }
        

        上面的代码应该只在你选择yes或no时关闭表单,当你选择yes时会保存数据。

        【讨论】:

          【解决方案4】:

          你可以试试这个:

          if (MessageBox.Show("Are you sure you want to quit?", "Attention!!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
          {
             //this block will be executed only when Yes is selected
             MessageBox.Show("Data Deleted", "Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
          }
          else
          {
            //this block will be executed when No/Cancel is selected
            //the effect of selecting No/Cancel is same in MessageBox (particularly in this event)
          }
          

          如果需要,您可以使用 DialogResult 类对 No 和 Cancel 按钮单击执行相同操作

          【讨论】:

            【解决方案5】:

            你应该试试这个功能

            public DialogResult msgClose(string msg)
            {
                 return MessageBox.Show(msg, "Close", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            }
            

            并像这样使用。

            private void frm_FormClosing(object sender, FormClosingEventArgs e)
            {
                 if (conn.msgClose("Application close?") == DialogResult.No)
                     e.Cancel = true;
                 else
                 {
                     this.Close();
                 }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-07-14
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多