المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Modeless active window/class - Cannot access a disposed object.



C# Programming
08-19-2009, 11:22 PM
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[^ (http://www.codeproject.com/Messages/3043750/Re-two-forms.aspx)]

Bad = knowing 2 much