Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何在关闭表单之前将消息从一个表单发送到另一个表单?_C#_.net_Forms - Fatal编程技术网

C#如何在关闭表单之前将消息从一个表单发送到另一个表单?

C#如何在关闭表单之前将消息从一个表单发送到另一个表单?,c#,.net,forms,C#,.net,Forms,我有一个程序,我希望它在关闭前请求确认。它只是一个简单的表单,带有问题和一个yes和一个no按钮。如何将单击哪个按钮的信息发送回主窗体?我找到的所有解决方案都是用于与打开的两个窗体进行通信,但当在第二个窗体中选择一个按钮时,它将关闭。任何提示或想法?您描述的第二种形式类似于消息框…您可以将其直接实现用作对话框。未经测试的示例: DialogResult dr = MessageBox.Show("Are you Sure?", "Confirm Exit?", MessageBo

我有一个程序,我希望它在关闭前请求确认。它只是一个简单的表单,带有问题和一个
yes
和一个
no
按钮。如何将单击哪个按钮的信息发送回主窗体?我找到的所有解决方案都是用于与打开的两个窗体进行通信,但当在第二个窗体中选择一个按钮时,它将关闭。任何提示或想法?

您描述的第二种形式类似于
消息框
…您可以将其直接实现用作对话框。未经测试的示例:

DialogResult dr = MessageBox.Show("Are you Sure?",
    "Confirm Exit?",
    MessageBoxButtons.YesNo);
if (dr==DialogResult.Yes)
{
    // Do work If Yes
}else //if( dr == DialogResult.No)
{
    // Do work if No
}

我倾向于这样做

子窗体的代码:

        private bool _bDoSomething=false;

        public bool showForm()
        {
            this.ShowDialog();
            return _bDoSomething;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            _bDoSomething=true;
            this.Hide();
        }
然后是父窗体的这类代码:

        dlgMyForm dlgMyForm = new dlgMyForm();
        if (dlgMyForm.showForm())
        {
          //do something
        }

将主窗体中的布尔值声明为公共

 public Boolean check =false;
在第二个表单的FormClosing事件中,执行以下操作

 private void Form2_FormClosing(Object sender, FormClosingEventArgs e) 
{
   DialogResult answer = MessageBox.Show("[Your text]",MessageBoxButtons.YesNo)

   if(answer == DialogResult.Yes)
     {

       Form1.check=True; //if button yes is clicked 
                         // set the form1 check variable to True and closes form2

     }
   else
     {
          Form1.check=False; //if button no is clicked 
                           // set the form1 check variable to False and cancel form 
                           // closing                                  
          e.Cancel=True;
     }


}

使用布尔变量检查在form1中对关闭事件进行进一步处理,在主窗体上设置属性。Javascript确认弹出框是否足够?对不起,我是新来的。你的简单句子对我来说毫无意义。我不知道如何在C#中使用Javascript,而且我的程序是葡萄牙语的,所以最好是有葡萄牙语选项的按钮,而不是英语。但是如果是葡萄牙语的,一个“Comfirm Box”就足够了,我知道如何使用它。谢谢。我想它解决了我的问题。我不知道消息框有“是或否”选项。如果可以的话,我会尝试,我会在明天再选择一次。