Modeless active window/class - Cannot access a disposed object.
-
it has problay discussed lots of times before, but i can't find the solution.
private void button1_Click(object sender, EventArgs e)
{
Form2 mw2 = new Form2();
mw2.Show();
}this way keeps creating new instances of the same class and therefor new windows/forms. i would like to do it as in the code below, create only one instance and keep it active and if i like to see it, make it visisble again. i would like to creatre a sort of test window, where i can see in a richtext what my main class is doing. thnx for reading in advance.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Modales_window
{
public partial class Form1 : Form
{
Form2 mw2 = new Form2();// Form1 form = (Form1)Application.OpenForms\[0\]; // Form2 form2 = (Form2)Application.OpenForms\[1\]; public Form1() { mw2.closingeventhandler += new Form2.ClosingEventHandler(mw2\_closingeventhandler); InitializeComponent(); } void mw2\_closingeventhandler() { //mw2.Dispose(); } private void button1\_Click(object sender, EventArgs e) { // Cannot access a disposed object. // Object name: 'Form2'. if (!mw2.Created) { mw2.Show(); } else { MessageBox.Show("already open", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void button2\_Click(object sender, EventArgs e) { textBox1.Text = Application.OpenForms.Count.ToString(); }
}
http://www.codeproject.com/Messages/3043750/Re-two-forms.aspx[^]
Bad = knowing 2 much
-
it has problay discussed lots of times before, but i can't find the solution.
private void button1_Click(object sender, EventArgs e)
{
Form2 mw2 = new Form2();
mw2.Show();
}this way keeps creating new instances of the same class and therefor new windows/forms. i would like to do it as in the code below, create only one instance and keep it active and if i like to see it, make it visisble again. i would like to creatre a sort of test window, where i can see in a richtext what my main class is doing. thnx for reading in advance.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Modales_window
{
public partial class Form1 : Form
{
Form2 mw2 = new Form2();// Form1 form = (Form1)Application.OpenForms\[0\]; // Form2 form2 = (Form2)Application.OpenForms\[1\]; public Form1() { mw2.closingeventhandler += new Form2.ClosingEventHandler(mw2\_closingeventhandler); InitializeComponent(); } void mw2\_closingeventhandler() { //mw2.Dispose(); } private void button1\_Click(object sender, EventArgs e) { // Cannot access a disposed object. // Object name: 'Form2'. if (!mw2.Created) { mw2.Show(); } else { MessageBox.Show("already open", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void button2\_Click(object sender, EventArgs e) { textBox1.Text = Application.OpenForms.Count.ToString(); }
}
http://www.codeproject.com/Messages/3043750/Re-two-forms.aspx[^]
Bad = knowing 2 much
You need to hook up to the closing event in form2, so that form1 knows when form2 is closed, and resets it to null. Then use a null check to create a new form. OR change the code in form2 so that it never gets closed, it just hides itself.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
You need to hook up to the closing event in form2, so that form1 knows when form2 is closed, and resets it to null. Then use a null check to create a new form. OR change the code in form2 so that it never gets closed, it just hides itself.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
wel, my goal was to keep the class going. as a sort of debugging window and make a reapear when needed. using the same button that created the original window. i know there are other possibilites to do this. by creating a simple class, store data, a array to hold the classes and create a new instance of Form2. Form2 contains a RichTextBox so i can display things of interest to me. i added a FormClosing Event + a Callback in Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;namespace Modales_window
{
public partial class Form2 : Form
{
public delegate void ClosingEventHandler();
public event ClosingEventHandler closingeventhandler;public Form2() { InitializeComponent(); } protected virtual void OnClosingEventHandler() { if (closingeventhandler != null) { closingeventhandler(); } } private void ModalesWindow2\_FormClosing(object sender, FormClosingEventArgs e) { OnClosingEventHandler(); } }
}
but this solution doesn't prevent the 'Cannot access a disposed object' error
mw2.Dispose();
public Form1() { mw2.closingeventhandler += new Form2.ClosingEventHandler(mw2\_closingeventhandler); InitializeComponent(); } void mw2\_closingeventhandler() { mw2.Dispose(); // this is how i tried to free up the recources. // but this causes a loop error : ( }
now i tried this, the same button creates/togles the visibillity of the window. but once you close Form2 with the 'close' button of the form i return to 'Cannot access a disposed object' error
private void button1\_Click(object sender, EventArgs e) { // Cannot access a disposed object. // Object name: 'Form2'. if (!mw2.Created) { mw2.Show(); } else { //MessageBox.Show("already open", "", MessageBoxButtons.OK, MessageBoxIcon.Information); if (mw2.Visible == true) { mw2.Visible = false; } else { mw2.Visi
-
You need to hook up to the closing event in form2, so that form1 knows when form2 is closed, and resets it to null. Then use a null check to create a new form. OR change the code in form2 so that it never gets closed, it just hides itself.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
i found 1 solution
private void ModalesWindow2\_FormClosing(object sender, FormClosingEventArgs e) { OnClosingEventHandler(); e.Cancel = true; // <--- surpresses the cancel event this.Hide(); }
works flawles. : ) but now i like to know the second answer. how to work around 'Cannot access a disposed object'
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Modales_window
{
public partial class Form1 : Form
{Form2 mw2 = new Form2(); // Form1 form = (Form1)Application.OpenForms\[0\]; // Form2 form2 = (Form2)Application.OpenForms\[1\]; public Form1() { mw2.closingeventhandler += new Form2.ClosingEventHandler(mw2\_closingeventhandler); InitializeComponent(); } void mw2\_closingeventhandler() { //mw2.Dispose(); //mw2.Hide(); } private void button1\_Click(object sender, EventArgs e) { // Cannot access a disposed object. // Object name: 'Form2'. if (!mw2.Created) { mw2.Show(); } else { //MessageBox.Show("already open", "", MessageBoxButtons.OK, MessageBoxIcon.Information); if (mw2.Visible == true) { mw2.Visible = false; } else { mw2.Visible = true; } } } private void button2\_Click(object sender, EventArgs e) { textBox1.Text = Application.OpenForms.Count.ToString(); GetOpenFormTitles(); } private void GetOpenFormTitles() { Collection<string> formTitles = new Collection<string>(); try { foreach (Form f in Application.OpenForms) { // Use a thread-safe method to get all form titles. formTitles.Add(GetFormTitle(f)); } } catch (Exception ex) { formTitles.Add("Error: " + ex.Message); } this.list
-
i found 1 solution
private void ModalesWindow2\_FormClosing(object sender, FormClosingEventArgs e) { OnClosingEventHandler(); e.Cancel = true; // <--- surpresses the cancel event this.Hide(); }
works flawles. : ) but now i like to know the second answer. how to work around 'Cannot access a disposed object'
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace Modales_window
{
public partial class Form1 : Form
{Form2 mw2 = new Form2(); // Form1 form = (Form1)Application.OpenForms\[0\]; // Form2 form2 = (Form2)Application.OpenForms\[1\]; public Form1() { mw2.closingeventhandler += new Form2.ClosingEventHandler(mw2\_closingeventhandler); InitializeComponent(); } void mw2\_closingeventhandler() { //mw2.Dispose(); //mw2.Hide(); } private void button1\_Click(object sender, EventArgs e) { // Cannot access a disposed object. // Object name: 'Form2'. if (!mw2.Created) { mw2.Show(); } else { //MessageBox.Show("already open", "", MessageBoxButtons.OK, MessageBoxIcon.Information); if (mw2.Visible == true) { mw2.Visible = false; } else { mw2.Visible = true; } } } private void button2\_Click(object sender, EventArgs e) { textBox1.Text = Application.OpenForms.Count.ToString(); GetOpenFormTitles(); } private void GetOpenFormTitles() { Collection<string> formTitles = new Collection<string>(); try { foreach (Form f in Application.OpenForms) { // Use a thread-safe method to get all form titles. formTitles.Add(GetFormTitle(f)); } } catch (Exception ex) { formTitles.Add("Error: " + ex.Message); } this.list
You found the first solution - cancel the closing event and hide it instead. This does not require a delegate. If your delegate does fire, then you need to set your instance of Form2 inside Form1 to null, and then your code will find that it is null and create a new instance when it wants to show it. This fixes the 'accessing disposed object. error b/c you instead know to create a new one. However, it will mean that you lose any data, so the solution you're using is the right one, I think.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
You found the first solution - cancel the closing event and hide it instead. This does not require a delegate. If your delegate does fire, then you need to set your instance of Form2 inside Form1 to null, and then your code will find that it is null and create a new instance when it wants to show it. This fixes the 'accessing disposed object. error b/c you instead know to create a new one. However, it will mean that you lose any data, so the solution you're using is the right one, I think.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
This does not require a delegate. this delgate was there before i asked the question : ) then you need to set your instance of Form2 inside Form1 to null how to set a class to NULL or destroy it? p.s. i also bought a mac, i wanted to know how it's like. after 30 minutes of usage i already saw it was far superior in usage. so many handy gadgets that make profesional users more productive. : ) p.p.s i'm not a proefesional coder, i'm more advanced than a beginner due to the years i have been doing it, but i learned alot last night.
Bad = knowing 2 much