2013-03-14 70 views
1

请帮我解决这个表单关闭后点击按钮执行,因为我只依靠windows关闭按钮(右上角),我没有使用额外的按钮来关闭表格。 但是,这个程序仍然在工作,但不会在保存.ini文件后立即自动关闭表单。表格关闭之后:button.performclick

我希望表单在button1.performclick()后关闭...但我不知道该怎么办..

我有以下代码:

int beFore, afTer; 
    private void Form3_Load(object sender, EventArgs e) 
    { 
     beFore = this.checkedListBox1.CheckedIndices.Count + 
       this.checkedListBox2.CheckedIndices.Count + 
       this.checkedListBox3.CheckedIndices.Count + 
       this.checkedListBox4.CheckedIndices.Count; 
    } 
    //private Form4 subForm4 = new Form4(); 
    private void Form3_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     afTer = this.checkedListBox1.CheckedIndices.Count + 
       this.checkedListBox2.CheckedIndices.Count + 
       this.checkedListBox3.CheckedIndices.Count + 
       this.checkedListBox4.CheckedIndices.Count; 
     while (beFore != afTer) 
     { 
      if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning", 
       MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) 
      { 
       this.button1.PerformClick(); //need to close this form after button.performclick.. 
       this.UpdateForm1(); 
       break; 
      } 
      else 
      { 
       this.UpdateForm1(); 
       break; 
      } 
     } 
     beFore = afTer; 

    } 

    private void UpdateForm1() 
    { 
     Form4 subForm4 = new Form4(); 
     subForm4.Show(); 
     subForm4.Update(); 
     try 
     { 
      int checkagain1 = this.checkedListBox1.CheckedIndices.Count; this.checkedListBox1.SetItemChecked(2, true); 
      int checkagain2 = this.checkedListBox2.CheckedIndices.Count; this.checkedListBox2.SetItemChecked(2, true); 
      int checkagain3 = this.checkedListBox3.CheckedIndices.Count; this.checkedListBox3.SetItemChecked(2, true); 
      int checkagain4 = this.checkedListBox4.CheckedIndices.Count; this.checkedListBox4.SetItemChecked(2, true); 

      Form1 myParentForm = (Form1)this.Owner; 
      if (myParentForm.comboBox1.Text.Length != 0) 
      { 
       //myParentForm.Enabled = false; 
       myParentForm.method1(); 
       myParentForm.method2(); 
       myParentForm.method3(); 
       myParentForm.method4(); 
       //myParentForm.Enabled = true; 
       subForm4.Close(); 
      } 

     } 
     catch (Exception) 
     { 
      subForm4.Close(); 
      return; 
      throw; 
     } 
    } 
+0

如果你想人们可以帮助你,请花更多的精力来格式化你的代码。我可以理解,英语很可能是你的第二语言,但你很难理解,问题不清楚。 – 2013-03-14 14:24:16

+0

什么是错误? – 2013-03-14 14:24:20

+0

我很抱歉的朋友,但目前还不清楚你想要做什么。我们看到了代码,并且隐约地理解了您对表单关闭时点击某个按钮所说的内容,但是您并没有清楚地解释***到底是什么。*** – 2013-03-14 14:25:27

回答

1

我不是100%肯定,你想要什么,但我敢肯定,你正在寻找的答案是形式的DialogResult属性。

如果您的问题是一个按钮正在关闭窗体,当您单击它,然后为窗体的DialogResult属性设置DialogResult.None。

DialogResult = DialogResult.None; 
+0

非常感谢...但是使用dialogresult.none我使用dialogresult.yes ... after.button1.performclick()... – job 2013-03-14 14:45:25

+0

这就是您在那里使用的弹出窗体的DialogResult。 Form4也有一个DialogResult。我是否正确地说你的问题是你的表单正在关闭,你不希望它成为? – James 2013-03-14 14:59:18

+0

no ..i想要保存文件后立即关闭窗体。然而,这两行代码工作...'this.button1.PerformClick();''this.DialogResult = DialogResult.Yes;' – job 2013-03-14 15:05:02

0

我没有正确理解你,但如果你想关闭窗体如果结果是肯定的,你可以这样做。

if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning", 
       MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) 
      { 
       this.Dispose(); 
       this.Close(); 
      } 

,如果你想这这种地方处置清洁程序使用,然后将其关闭,或者关闭关闭窗体直接

或尝试做这一切的资源:

private void button1_Click(object sender, EventArgs e) 
     { 
      this.Dispose(); 
      this.Close(); 
     } 

    if (MessageBox.Show("Changes have been made..\r\nSave to configuration file (.ini) ?", "Warning", 
        MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) 
       { 
        button1.PerformClick(); 
       } 
+0

抱歉,如果你不明白...因为这是我第一次在这里发布..但来自“judgeja”的答案......正在进行一些修改,如下所示Dialogresult = DialogResult.Yes; – job 2013-03-14 14:59:33