Mario wrote:
Hi,
I am trying to create an application with multiple windows forms. The
problem that I have is that after creating the window forms, I do not
know how to open formN after closing Main form. Each form is in its
own class. What I want to do is the following:
1. Click a button on Main menu.
2. Close main menu ( I would use hide if possible)
3. Open form2
4. Open other forms and hide or close the previous form.
5. Eventually able to go back to Main form.
I have been experimenting with creating new forms from Main form and
closing/disposing them. However, I would like to learn how to use
forms that I can create using the windows forms that I have created.
Any help is appreciated. Thanks.
Mario
You need to subscribe to the FormClosing or FormClosed event of your
child on your main form or set a delegate on your child forms which gets
invoked in the closing method of your child forms.
main form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdChild_Click(object sender, EventArgs e)
{
frmChild Child = new frmChild();
Child.FormClosed += new FormClosedEventHandler(Child_FormClosed);
Child.Show();
this.Hide();
}
void Child_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show();
}
private void cmdChildDelegate_Click(object sender, EventArgs e)
{
frmChildDelegate Del = new frmChildDelegate(new
MethodInvoker(delegate { this.Show(); }));
Del.Show();
this.Hide();
}
}
Delegate child Form:
public partial class frmChildDelegate : Form
{
MethodInvoker ShowParent;
public frmChildDelegate(MethodInvoker showParent)
{
ShowParent = showParent;
InitializeComponent();
}
private void frmChildDelegate_FormClosing(object sender,
FormClosingEventArgs e)
{
ShowParent();
}
}