對於Windows窗體,我不是經常性的,但通常在C#中會變得更好。我正在開發一個comp的項目。 PROG。類,它是一個允許多個子窗體的MDI表單。如何在MDI項目中關閉子窗體時停止計時器事件
這是我的泡菜, 我在父母的窗體上有一個計時器;當勾選時,處理兩個標籤方法..一個用於計算文本文檔中的字符,另一個用於顯示縮放級別。
我可以讓計時器觸發和處理我的事件,當一個孩子的窗戶打開,但當我關閉窗口,我想弄清楚如何停止計時器一些如何。 我已經嘗試了form.closing事件,並嘗試禁用計時器,當我完成,但這並沒有幫助。
該項目是一個文本編輯器和對象的名稱ID「文檔」。當對象被處置時,自然我會得到一個異常,但是我想在這種情況發生之前禁用定時器。
「無法訪問已釋放的對象」
這是我的新()方法調用子窗體的一個實例..
void New()
{
// Generate a new form from scratch
TextEditorChild = new Form(); // Declare a variable containing a new Form method
TextEditorChild.Text = "Document " + count.ToString(); // Text Property - also gets the forms order number
TextEditorChild.Icon = Properties.Resources._new_doc_icon; // Use our own icon
TextEditorChild.MdiParent = this; // Ensure we are using the original form as the parent form
Document = new RichTextBox(); // Call a new RichTextBox object
Document.Multiline = true; // Yes, a multiline textbox
Document.Dock = DockStyle.Fill; // Ensure that the textbox fills the new window
TextEditorChild.Controls.Add(Document); // Apply our controls to the child window
TextEditorChild.Show(); // Display the window
count++; // Add this window to a potnetial list of windows, should multiple be opened all at once
timer.Enabled = true;
}
這裏是我的計時器,甚至處理...
private void timer_Tick(object sender, EventArgs e)
{
charCount.Text = "Characters in the current document: " + Document.TextLength.ToString();
zoom.Text = Document.ZoomFactor.ToString();
}
你打開了多個TextEditorChild還是隻打開一個? – Steve
它只拋出一個異常。 實際上,我發佈的代碼不會允許定時器開始。 我的第一個刺是創建和IF語句和 ' if(TextEditorChild == null) { return; } 其他 {// 方法 } ' 這個工作,但是當我關閉子窗口,我再次得到了消息。 –