对于Windows窗体,我不是很普通,但总体而言,在C#方面仍然会变得更好。我正在开发一个补偿项目。编 类,它是允许多个子表单的MDI表单。
这是我的泡菜,在父窗体上有一个计时器;勾选时,处理两种标签方法..一种用于计数文本文档中的字符,另一种用于显示缩放级别。
当子窗口打开时,我可以启动计时器并处理事件,但是当我关闭窗口时,我试图弄清楚如何停止计时器。我尝试了form.closing事件,并在完成后尝试禁用计时器,但这无济于事。
该项目是一个文本编辑器,对象的名称ID为“文档”。当该对象被Dispose时,自然会得到一个异常,但是我想在发生这种情况之前禁用计时器。
“无法访问已处置的对象”
这是我的New()方法,用于调用子窗体的实例。
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.FormClosing += new FormClosingEventHandler(Close);
在方法new()中
private void Close(object sender, FormClosingEventArgs e)
{
timer.Enabled = false;
}
并将其作为新方法添加到下面
这样一来,当窗体关闭时,它将停止计时器,然后退出窗体
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句