I have a windows application which has a main form (Home screen) and many subforms.
When the subform is closed and called back again from the main form the System.ObjectDisposedException exception occurs.
Below are my screen codes listed :
Home screen code calling the Subform:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Application
{
public partial class Home : Form
{
private void Businesslogic_button_Click(object sender, EventArgs e)
{
BusinessRules.Show();
}
}
}
Subform Designer.CS code for disposing objects:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Subform .CS code for the form closing event:
bool formClosing false;
private void BusinessRules_FormClosing(object sender, FormClosingEventArgs e)
{
if (formClosing) return;
e.Cancel = true;
Timer Tmr = new Timer();
Tmr.Tick += Tmr_Tick;
Tmr.Start();
formClosing = true;
}
void Tmr_Tick(object sender, EventArgs e)
{
((Timer)sender).Stop();
this.Close();
}
Try this in Subform.CS :
private void Subform_FormClosing( object sender, FormClosingEventArgs e )
{
e.Cancel = true;
this.Hide();
}
If you need to store the state of the form, simply call the Hide() method and set e.Cancel = true. Then just call Show() on the form variable again to re-open it.
If you don't want to retain the state, simply close the form. And open a fresh instance of the form from your main page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.