显示表单时出现 ObjectDisposedException

ObjectDisposedException while Showing a Form

当我需要显示一个表单时,我不断收到 ObjectDisposedExpection。 你们知道怎么做吗?在 NotitiesForm.Show() 的 foreach 中,我收到错误 ObjectDisposedExpection。我正在使用 Visual Studio Ultimate 2012 C# 进行编程。

RichTextBox NotitiesTB = new RichTextBox();
private Form NotitiesForm;

/// <summary>
/// 
/// </summary>
/// <param name="label"></param>
/// 
public void NotitiesLatenZien()
{
    if (filename != null)
    {                
        BRTSignal signal = new BRTSignal(filename);
        BRTEventRepository Notities = new BRTEventRepository(signal);

        List<IBRTNote> note = Notities.ReadNotes();

        BRTEventService TijdNotities = new BRTEventService(signal);
        TijdNotities.MakeNoteTimesRelativeToTrack(note, 1);

        //TextBox NotitiesTB = new TextBox();
        //NotitiesTB.Name = "Notities";

        if (NotitiesForm == null)
        {
            NotitiesForm = new Form();
        }              

        NotitiesForm.Height = 600;
        NotitiesForm.Width = 1000;
        NotitiesForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        NotitiesForm.MaximizeBox = false;
        NotitiesForm.Disposed +=NotitiesForm_Disposed;

        NotitiesForm.Text = "Notities";

        NotitiesTB.Multiline = true;
        NotitiesTB.Height = 600;
        NotitiesTB.Width = 980;
        NotitiesTB.ReadOnly = true;

        NotitiesTB.Clear();

        //NotitiesTB.Click += NotitiesTB_Click;

        //NotitiesTB.SelectionStart = Convert.ToInt32(referenceLineSelectedPage);

        NotitiesTB.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Both;

        NotitiesTB.Name = "Notities";

        NotitiesForm.Controls.Add(NotitiesTB);

        foreach (IBRTNote notes in Notities.ReadNotes())
        {
            //string test = Convert.ToString((notes.Time));
            //textBox1.Text = String.Concat(textBox1.Text, string.Concat(Environment.NewLine, notes.Text));
            if (NotitiesTB.Text == "")
            {
                NotitiesTB.Text += new BRTToDotNET.RTDateTime((long)notes.Time).ToDotNet().ToString() + "     " + notes.Text;
            }
            else
            {
                NotitiesTB.Text += "\r\n" + new BRTToDotNET.RTDateTime((long)notes.Time).ToDotNet().ToString() + "     " + notes.Text;
            }
            //MessageBox.Show("\r\n" + notes.Text);
            NotitiesForm.Show();
            NotitiesForm.BringToFront();
        }
    }
    else
    {
        MessageBox.Show("Er blijkt een .sig file te missen. Controleer of u een .sig file heeft ingeladen.");
    }
}

private void NotitiesForm_Disposed(object sender, EventArgs e)
{
    NotitiesForm = null;
} 

您发布的代码似乎 "good enough"。也就是说,您在对象被释放时设置 NotitiesForm 变量,如果它为空则创建一个新变量。只要所有这些代码都在主 UI 线程中执行,那部分就没问题。

但请注意,当 Form 被释放时,Form 中的所有控件都会被释放。因此,您的 NotitiesTB 控件将在您的 NotitiesForm 第一次关闭时被处理掉。然后将该控件添加到您创建的下一个 Form 实例,当它显示时,您会得到异常,因为您试图显示一个包含已被释放的控件的 Form

正确的做法是设计一个实际的 Form 子类 ,它已经包含您想要的 RichTextBox 实例。然后,您不必为您创建的 Form 的每个新实例添加一个新实例。

除此之外,您需要创建一个新的 RichTextBox 实例以配合您创建的每个新 Form 实例,例如在你有 NotitiesForm = new Form(); 声明的同一个地方。