0

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

4

3 回答 3

1

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

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
}

请参阅 MSDNMessageBox

于 2013-06-20T01:54:37.710 回答
0

我倾向于这样做。

子窗体代码:

        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
        }
于 2013-06-20T01:58:18.053 回答
0

将主要形式的布尔值声明为公共

 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中做进一步处理

于 2013-06-20T06:36:15.927 回答