显示表单时出现ObjectDisposedException

时间:2015-01-26 11:58:54

标签: c# winforms objectdisposedexception

当我需要显示一个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;
} 

1 个答案:

答案 0 :(得分:1)

您发布的代码似乎足够好&#34;。也就是说,您在放置对象时设置NotitiesForm变量,如果它是null,则创建一个新变量。只要所有这些代码都在主UI线程中执行,那部分就可以了。

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

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

除此之外,您需要创建一个新的RichTextBox实例,以便与您创建的每个新Form实例一起使用,例如:在您拥有NotitiesForm = new Form();声明的同一个地方。