2012-01-31 45 views
0

的FormClosed事件我有這個按鈕的代碼:處理中的WinForms

private void button22_Click_1(object sender, EventArgs e) 
    { 
     Separare sp = new Separare(dataGridView1,label_pin.Tag.ToString(),label_pin.Text); 
     sp.FormClosed += new FormClosedEventHandler(ClosedForm); 
     sp.Show(); 

    } 

的FormClosedEventHandler看起來是這樣的:

DataTable bon_temp = bon_tempTableAdapter.GetDataByTable(label_pin.Tag.ToString()); 

     foreach (DataRow row in bon_temp.Rows) 
     { 
      AddRow(row.ItemArray[3].ToString(), Convert.ToInt32(row.ItemArray[4]), Convert.ToDecimal(row.ItemArray[5])); 
      Console.WriteLine(row.ItemArray[3].ToString(), Convert.ToInt32(row.ItemArray[4]), Convert.ToDecimal(row.ItemArray[5])); 
     } 

     bon_tempTableAdapter.DeleteQuery(label_pin.Tag.ToString()); 

其中AddRow方法添加行的DataGridView。我的問題是,當我關閉SP表格行不添加到DataGridView。每當用戶關閉後形式已被關閉形式,並指定關閉原因發生

+0

你爲什麼在WinForms應用程序寫入到控制檯? – 2012-01-31 14:57:10

+0

可能不是使用Debug.WriteLine,因爲在WinForms應用程序中,Console.WriteLine會寫入VS控制檯。 – 2012-01-31 15:00:10

+0

AddRow在哪裏定義? – 2012-01-31 15:00:37

回答

6

FormClosed

的原因爲您的代碼不工作可能是在窗體上的某些控件已經被破壞......

我建議你使用FormClosing事件每當用戶關閉窗體時發生,形式面前已關閉並指定關閉原因。

示例代碼(這是非常相似,你在上面做了什麼):

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MyMainForm_FormClosing); 

... 

private void MyMainForm_FormClosing(object sender, FormClosingEventArgs e) 
{ 
    //your code goes here 
    //optionally, you can get or set e.Cancel which gets or sets a value indicating that the event should be cancelled; in this case the form won't close if you cancel it here 
    //or, you can check e.CloseReason which gets a value that indicates why the form is being closed (this is an enum Systems.Windows.Forms.CloseReason) 
} 
+0

你能給我一個標準的FormClosing例子嗎? – 2012-01-31 15:35:29

+0

@Emil:我喜歡這樣的事情,並且喜歡類似的節目。 :)成功。 – woohoo 2012-01-31 15:50:56