2013-03-14 56 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