Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:13:13+00:00 2026-06-07T09:13:13+00:00

When I open a form which is created at runtime for the second time

  • 0

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();
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:13:15+00:00Added an answer on June 7, 2026 at 9:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm searching for an open source runtime form designer (for SCADA, not searching for
I created a simple asp.net form which allow users to view a list of
I have created a simple form which is loading a csv file with few
I have a form that contains 5 drop down lists which are created by
Why compiling this create an exe which : open a console launch the form
The first time I open one of my child forms from the main form
I've created a class library which has a method to query for open ports,
I have an Access form in which I am unsuccessful in formatting the record
I have a form which will open a new form when one button (form1button)
I created an Access database which I want to distribute to a small group.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.