【发布时间】:2014-01-25 08:30:48
【问题描述】:
我不是 Windows 窗体的常客,但总体上还是在 C# 方面做得更好。我正在为一个comp开发一个项目。编。类,它是一个允许多个子窗体的 MDI 窗体。
这是我的泡菜, 我在父表单上有一个计时器;勾选时,处理两种标签方法..一种用于计算文本文档中的字符,另一种用于显示缩放级别。
当子窗口打开时,我可以让计时器触发并处理我的事件,但是当我关闭窗口时,我试图弄清楚如何以某种方式停止计时器。 我尝试了 form.closure 事件,并尝试在完成后禁用计时器,但这没有帮助。
该项目是一个文本编辑器,对象的名称 ID 为“文档”。当对象被 Disposed 时,我自然会得到一个异常,但我想在这种情况发生之前禁用计时器。
“无法访问已释放的对象”
这是我调用子窗体实例的 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 还是只打开了一个?
-
它只抛出一个异常。实际上,我发布的代码根本不允许计时器开始。我的第一个刺是创建和 IF 语句和 ' if (TextEditorChild == null) { return; } else { // 方法 } ' 这有效,但是当我关闭子窗口时,我又收到了消息。