Hello , i made a form with a button. When i press this button , it open a new form. If i close the new form , and i try to open it again , i recive a error.

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Elemente'.
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Form.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.Show()
   at Mendeleev.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Hacker\My Documents\C# Projects\Mendeleev\Mendeleev\Form1.cs:line 26
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
    Assembly Version:
    Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    Assembly Version: 5.2.3331.35966
    Win32 Version: v5.2
    CodeBase: file:///C:/Documents%20and%20Settings/Hacker/My%20Documents/C%23%20Projects/Mendeleev.exe
    Assembly Version:
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/
    Assembly Version:
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/
    Assembly Version:
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging

For example:

    <system.windows.forms jitDebugging="true" />

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Code from Form ( that with button ) Here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Mendeleev
    public partial class Form1 : Form
        public Form1()

        Elemente hidrogen = new Elemente();
        //bool H;
        private void Button1_Click(object sender, EventArgs e)
            //if (!H)
                hidrogen.Size = new Size(267, 560);
                hidrogen.BackgroundImage = Properties.Resources.Hidrogen;
                hidrogen.Text = "Hidrogen";
           // }
            //H = true;

Edited by charqus: n/a

8 Years
Discussion Span
Last Post by ddanbe
Elemente hidrogen = new Elemente();

Move this inside of your button click handler. As you're doing it now, you're instantiating it outside of the handler and then showing it inside. When you close the form, the instance has been disposed. That's the error you're getting, trying to work with a disposed instance. If the instance is local to the click handler, you do not have this problem.


Oh yeah , you right , thanks ... But this will cause a issue too ... if a press click on button it open the form , if i press again , it open a new form ... why ?


You're right, I had not considered that. Since the variable is local, each click event creates a new variable and a new instance, so you get multiple forms.

Change of plan. Consider this example:

Form2 form2 = null;

        private void button1_Click(object sender, EventArgs e)
            if (form2 == null || form2.IsDisposed)
                form2 = new Form2();


In it, the declaration is outside of the click handler, but the instantiation is still inside of it, but it only happens if form2 is null or has been disposed.

Edited by apegram: n/a


Should have posted this in a new thread.
Set the FormBorderStyle property of your form to FixedSingle.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.