ObjectDisposedException when opening a form - c#

Everything works as it should when I open the form the first time with Form.show(); but after I close it with Form.Close(); and try to re-open it I get a 'ObjectDisposedException'. What do I need to do to avoid this if I need to open the form for more than one time?

You will need to instantiate a new Form after closing and disposing of the existing instance.
Form form = new Form();
form.Show();

You can use
form.Hide();
this wil just hide the form from the user instead of disposing it.
Keep in mind that if the user closes the form, it again will be disposed, so you could prevent that with
public Form()
{
InitializeComponent();
this.FormClosing += Form_FormClosing;
}
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
e.Cancel = true;
}

Related

Form remains open in the background after closing it

So I want to show a form and close the other form so I did this in form1:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Show();
this.Hide();
//didn't use this.close() cause then the whole program will close instead of
//just the current form
}
Then I wanted the main form to open again after the 2nd form is closed so I did this in form2:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 x = new Form1();
x.Show();
}
Now the problem is when I'm back in the main form after closing the 2nd form. if I close the main form it doesn't fully close and still remains open in the background (I found it in task manager). I think its because the "show" method just opens another form instead of making the form appear so the main form which got hidden is still there running in the background.
what should I do to make the form get closed when I exit it?
I tried putting this.close(); in the form closed and form closing event but both resulted in a crash.
When you write:
Form1 x = new Form1();
you are creating a new Form1 object, so x refers to the new one and not to the original one. Instead, you could use this:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var form2 = new Form2())
{
this.Hide();
form2.ShowDialog();
}
this.Show();
}
When ShowDialog() is called, the code following it is not executed until after the dialog box is closed, so this.Show() will execute only after Form2 is closed.
Another option is to simply subscribe to the FormClosed() event of Form2 when you create it, then un-hide your instance of Form1 from there:
// ... all in Form1 ...
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form2 x = new Form2();
x.FormClosed += X_FormClosed;
x.Show();
}
private void X_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show();
}
So then when you close Form2 your instance of Form1 will automatically re-appear.

C# Switching forms with passed data in the constructor

I'm searching for the proper way how to open and switch forms some uses Application.Run( new Form1());, some uses Form1.ShowDialog(); and Form.Show();. I really want to know how to properly pass a data from a form into another using constructors.
Additionally I want to know why when I use Form.Close(); to close to current form before to open next form .... both forms closes.
Here are my codes.
try
{
Form2 f2 = new Form2(connection, userLogin);
this.Hide();
f2.ShowDialog();
}
catch (NullReferenceException nre) {
MessageBox.Show("Sorry Login Another User account");
}
What I'm trying to do here is to pass the MySqlConnection in the variable connection and the valid user in the variable userLogin into the Form2. This method works but I'm not sure if this the right way to do it.
Here are the codes in Form2.
public partial class Form2 : Form
{
MySqlConnection connection;
User activeUser;
public Form2(MySqlConnection pConn, User loginUser)
{
InitializeComponent();
connection = pConn;
activeUser = loginUser;
this.Init();
this.CenterToScreen();
}
private void logoutB_Click(object sender, EventArgs e)
{
this.Hide();
new Form1().Visible = false;
new Form1().ShowDialog();
//Application.Run(new Form1());
}
}
Showing the Form2 doesn't also have a problem but when the logout button is pressed.
Form that is already visible cannot be displayed as a modal dialog box.
Set the form's visible property to false before calling showDialog.
It says Form already visible? So does it mean the form is still open even though I used this.Hide();? and when I use the code Application.Run(new Form1());
Starting a second message loop on a single thread is not a valid operation.
Use Form.ShowDialog instead.
In this section of code:
new Form1().Visible = false;
new Form1().ShowDialog();
You are simply creating two new forms; you are creating a form in the first line with Visibilityproperty set to false; and in the second line you are creating a new Form and calling ShowDialog on it. So two instances are not the same.
this.Hide();
Form1 a = new Form1();
a.Visible = false;
a.ShowDialog();
Since you use ShowDialog to show Form2 from Form1, then simply close the second form when the user clicks the logout button.
private void logoutB_Click(object sender, EventArgs e)
{
this.Close();
}
This will bring execution back to Form1 right after the ShowDialog call. So, make sure then you re-show the form after you invoke ShowDialog like this:
try
{
Form2 f2 = new Form2(connection, userLogin);
this.Hide(); //hide my self
f2.ShowDialog(); //show Form2.
//Execution will resume here after Form2 is closed
this.Show(); //re-show my self
}
catch (NullReferenceException nre)
{
MessageBox.Show("Sorry Login Another User account");
}

C# - Application continues running in the background when closing it after an another form was opened

When I click on About, a new form opens and the main form hides. I have a close button on the About button, that closes the About form, and it shows up the Main form. However, when I close the main form with the 'X' close button on top, the Main form closes, but the process is still running in the background. Everything is okay when I dont click on the About button. It kills the process.
In your Form2, you need to bring up the existing Form1 instance rather than creating a new one (that's why closing this newly created Form1 won't exit the application, as your old Form1 instance is still hiding...).
So you could get reference passed when creating aboutdialog, and show that instead when closing aboutdialog:
Form1:
private void AboutButton_Click(object sender, EventArgs e)
{
var aboutdialog = new Form2(this);
aboutdialog.Show();
this.Hide();
}
Form2:
Form1 _parentform;
public Form2(Form1 parent)
{
_parentform=parent;
InitializeComponent();
}
private void CloseButton_Click(object sender, EventArgs e)
{
_parentform.Show();
this.Close();
}
In your "AboutButton_Click" method you hide the Form1. But when closing the About dialog, you do not show back the Form1, but create a new one. The original Form1 stays hidden and prevents your application from closing.
better to use showdialog.
private void AboutButton_Click(object sender, EventArgs e)
{
var aboutdialog = new Form2();
aboutdialog.ShowDialog();
}
When form2 is closed a new instance of form1 is opened, instead of the original one. You could include a reference to Form1 inside of Form2, or use the default Ownerof a form, by showing the dialog with Form1 as its owner:
aboutdialog.Show(this);
in the about...click
and closing
private void CloseButton_Click(object sender, EventArgs e)
{
Owner.Show();
this.Close();
}

How can I close the MainForm, without closing the others?

So, I'm writing a Hangman, and at the MainForm you have to choose if you want to play single player or multiplayer. When I choose which one I want, this MainForm should close( I use this.Close() ) and trigger another Form, but instead the entire program shuts down.
private void button2_Click(object sender, EventArgs e)
{
Form f2 = new Form1();
f2.Show();
this.Close();
}
If in Programs.cs I modify the code like this:
Form f4 = new Form4();
f4.Show();
Application.Run();
everything goes well, but If I won't exit the program using Application.Exit(), it will still run in the background.
So, how could I solve this problem?
You can't close the parent form and keep the children alive.
Use this.Hide() instead of this.Close()
Then on the Form2_FormClosed Event you can do Application.Exit()
or you can even show the MainForm Again.
OR:
Form2 f2 = new Form2();
this.Visible = false;
f2.ShowDialog();
this.Close();
If you want a basic form that will pretty much just allow the user to play until they close the other form then close this form, use this:
private void button2_Click(object sender, EventArgs e)
{
Form f2 = new Form1();
this.Hide();
f2.ShowDialog();
this.Close();
}
or something a bit more fancy will allow you to close the form if the user selects something like not wanting to play another game or change difficulty settings or whatever. You can do that like this:
private void button2_Click(object sender, EventArgs e)
{
Form f2 = new Form1();
this.Hide();
if(f2.ShowDialog() == DialogResult.OK)
{
this.Show();
}
else
{
this.Close();
}
}
You can show (dialog) your form before Application.Run(new MainForm()), so you don't need to close the Mainform

Opening a new form, closing the old one C#

I'm kinda new to C# and I'm doing self study by trying to make a program with a variety of functions to teach myself how to work with C#. I usually look at the internet if I don't know something but this has been driving me crazy.
I remember in the very beginning i started this that I wanted to open a form and close the old one, but when i closed the new form, the old form would reappear again, and other weird varieties of this issue. this.Hide() didn't seem to do anything either.
Currently for opening a new form I'm using this code, but it feels like there should be something with 1 line of code for something as simple as opening a form...
My question is if there is.
private void OpenMainForm()
{
MainForm frm2 = new MainForm();
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
frm2.Show();
// Since this.Hide() for some reason doesn't work, i'll have to do this crap
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
If you want to hide your main window when you're in the secondary one, you should use the ShowDialog() method. With that, you won't even need the form_closed event.
Your code should look like:
private void OpenMainForm()
{
MainForm frm2 = new MainForm();
this.Hide(); //Hide the main form before showing the secondary
frm2.ShowDialog(); //Show secondary form, code execution stop until frm2 is closed
this.Show(); //When frm2 is closed, continue with the code (show main form)
}
You can also use this code:
public static void ThreadProc()
{
Form2 f;
Application.Run(new Form2());
}
private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
t.Start();
this.Close();
}
This works perfectly for me
Form2 frm = new Form2();
frm.Show();
frm.Activate();
this.Hide();
but if you want to close the whole application from Form2...you have to add Application.Exit(); in FormClosing event of Form2
You can hide old form as below.
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Hide();
}

Categories

Resources