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 **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3082 (QFE.050727-3000)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Mendeleev
    Assembly Version: 5.2.3331.35966
    Win32 Version: v5.2
    CodeBase: file:///C:/Documents%20and%20Settings/Hacker/My%20Documents/C%23%20Projects/Mendeleev.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** 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
enabled.

For example:

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

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()
        {
            InitializeComponent();            
        }

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

Recommended Answers

All 6 Replies

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();
            }

            form2.Show();
        }

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.

You're a genious , thank you very much

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.