Question showing new forms

l4gspike

New member
Joined
Jul 8, 2016
Messages
3
Programming Experience
Beginner
Good Day everyone,
I am writing my own web browser, just for something to do.
I am able to surf the web and all with it but when i try to add a history form i crash.

Well it works the first time and does record my history BUT as soon as i close the history window and try to open it again, well i crash. Heres the Error that i keep getting.

C#:
ObjectDisposedException was unhandled
C#:
An unhandled exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll




and here is the code that i wrote.



C#:
public partial class frmBrowser : Form
          { 
             public frmBrowser()
             {
                InitializeComponent();
             }

             [COLOR=#b22222]public History frmhis = new History();[/COLOR]

        private void NavigateToPage()
        {
            //takes url from toolbar text box and navigates to webpage
            txtURL.Enabled = false;
            btnGo.Enabled = false;
            wbBrowser.Navigate(txtURL.Text);
            txtURL.Items.Add(txtURL);
            txtStatlbl.Text = "Navigation has started";
[COLOR=#b22222]            frmhis.lstHistory.Items.Add(txtURL.Text);[/COLOR]
        }


C#:
[COLOR=#b22222]private void historyToolStripMenuItem_Click(object sender, EventArgs e)[/COLOR][COLOR=#b22222]         {[/COLOR]
[COLOR=#b22222]            frmhis.Show();[/COLOR]
[COLOR=#b22222]         }[/COLOR]


Everything in red has to do with the code in question.
Any help is appreciated.
Thanks in advance
L4gspike
 
When form is closed it is automatically disposed, and then your frmhis field variable just point to a disposed object.
 
If you display a form by calling Show, it is disposed when it is closed but it still exists. That means that you can still refer to it but you can't display it again. Here is what I consider to be the best way to keep a reference to a form while displaying a new instance each time you want to display it:
private SomeForm dialogue;

private void DisplayDialogue()
{
    // dialogue will be null the first time and IsDisposed will be true if the last instance has been closed.
    if (dialogue == null || dialogue.IsDisposed)
    {
        dialogue = new SomeForm();
    }

    // This will display the new instance if one is not already displayed.
    dialogue.Show();

    // This will focus the existing instance if one is already displayed.
    dialogue.Activate();
}
 
If you display a form by calling Show, it is disposed when it is closed but it still exists. That means that you can still refer to it but you can't display it again. Here is what I consider to be the best way to keep a reference to a form while displaying a new instance each time you want to display it:
private SomeForm dialogue;

private void DisplayDialogue()
{
    // dialogue will be null the first time and IsDisposed will be true if the last instance has been closed.
    if (dialogue == null || dialogue.IsDisposed)
    {
        dialogue = new SomeForm();
    }

    // This will display the new instance if one is not already displayed.
    dialogue.Show();

    // This will focus the existing instance if one is already displayed.
    dialogue.Activate();
}



Thanks alot it worked perfectly!
 
Back
Top Bottom