Tornado726
Tornado726

Reputation: 350

Can't open form a second time-keep getting error

When I open a form which is created at runtime for the second time , I keep getting an error saying "Cannot access a disposed object." "Object name 'Form'" Here is the code of the function. It is called by a buttonclick event. I have looked all over the Net and found people with similar problems, however I have tried those fixes and none of them work. Not sure how to fix this problem. VS highlights the line frmFavorites.Show();

Thank you in advance.

public void frmMyBrowser_ShowFavorites(object sender, EventArgs e)
{        
    frmFavorites.ShowIcon = false;
    frmFavorites.ShowInTaskbar = false;
    frmFavorites.Text = "Bookmarks";
    frmFavorites.Width = 500;
    frmFavorites.Height = 320;
    frmFavorites.Controls.Add(lstFavorites);
    frmFavorites.Controls.Add(btnRemoveFavorite);
    frmFavorites.Controls.Add(btnAddFavorite);
    frmFavorites.Controls.Add(txtCurrentUrl);
    lstFavorites.Width = 484;
    lstFavorites.Height = 245;
    btnRemoveFavorite.Location = new Point(397, 255);
    btnAddFavorite.Location = new Point(8, 255);
    txtCurrentUrl.Location = new Point(110, 255);
    txtCurrentUrl.Size = new Size(265, 20);
    btnAddFavorite.Text = "Add";
    btnRemoveFavorite.Text = "Remove";
    txtCurrentUrl.Text = wbBrowser.Url.ToString();
    btnAddFavorite.Click += new EventHandler(btnAddFavorite_Click);
    btnRemoveFavorite.Click += new EventHandler(btnRemoveFavorite_Click);
    frmFavorites.Load += new EventHandler(frmFavorites_Load);
    frmFavorites.Show();
    frmFavorites.FormClosed += new FormClosedEventHandler(frmFavorites_FormClosed);

    StreamReader reader = new System.IO.StreamReader(@Application.StartupPath + "\\favorites.txt");
    {
        while (!reader.EndOfStream)
        {
            for (int i = 0; i < 4; i++)
            {
                string strListItem = reader.ReadLine();
                if (!String.IsNullOrEmpty(strListItem))
                {
                     lstFavorites.Items.Add(strListItem);
                }
            }
        }
        reader.Close();   
    }        
}

public void btnAddFavorite_Click(object sender, EventArgs e)
{
    lstFavorites.Items.Add(wbBrowser.Url.ToString());
}

public void btnRemoveFavorite_Click(object sender, EventArgs e)
{
    try
    {
        lstFavorites.Items.RemoveAt(lstFavorites.SelectedIndices[0]);
    }
    catch
    {
        MessageBox.Show("You need to select an item", "Error");
    }
}
public void frmFavorites_Load(object sender, EventArgs e)
{       
}

public void frmFavorites_FormClosed(object sender, FormClosedEventArgs e)
{
    StreamWriter writer = new System.IO.StreamWriter(@Application.StartupPath + "\\favorites.txt");
    {
        for (int i = 0; i < lstFavorites.Items.Count; i++)
        {
            writer.WriteLine(lstFavorites.Items[i].ToString());
        }
        writer.Close();
    }
    frmFavorites.Close();
 }

Upvotes: 0

Views: 395

Answers (2)

Matthew Watson
Matthew Watson

Reputation: 109567

You cannot reopen a form that has been closed. When a form is closed (1) it is disposed and (2) the underlying operating system Window handle is freed.

I guess you might be able to take the contents of the Form's constructor and put it into a method that you could call to reopen the form, but I really wouldn't recommend doing that even if it does work.

Instead, just create a new instance of the form.

Upvotes: 2

JohnnBlade
JohnnBlade

Reputation: 4327

Try this

public void frmMyBrowser_ShowFavorites(object sender, EventArgs e)
{
  frmFavorites = new Form();

   ....
   ....

}

Upvotes: 2

Related Questions