It appears that form1 is not really closed, it's just hidden somewhere but somehow I cannot re-display it. I tried both form1.Show() and form1.Visible = true but nothing works. Here's my code:
public partial class Main : Form
{
Form1 form1;
public Main()
{
InitializeComponent();
}
private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (form1 != null)
{
form1.Show();
this.ActiveControl = form1;
form1.WindowState = FormWindowState.Normal;
form1.BringToFront();
return;
}
else if (form1 == null || form1.IsDisposed)
{
form1 = new Form1();
form1.MdiParent = this;
form1.TopLevel = false;
form1.Dock = DockStyle.Fill;
form1.Show();
this.ActiveControl = form1;
form1.BringToFront();
}
}
}
Henry VuongPosted Sep 17, 2008, 6:07 PM
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide(); // hide the form instead of closing
e.Cancel = true; // this cancels the close event.
}
This will hide form1 instead of closing it when user clicks the "x" icon at the top right corner of the form. I figure out the Show() method can only re-display a hidden form, not a closed or disposed form.
Ryan AlfordPosted Sep 16, 2008, 3:41 PM
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.MdiParent = this;
form1.TopLevel = false;
form1.Dock = DockStyle.Fill;
form1.Show();
this.ActiveControl = form1;
form1.BringToFront();
}
}
then on your child form, add the "FormClosed" Event
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
this.Dispose();
}