I want to be able to show, hide or close some other forms within one form. the idea is my program starts with one form and there are some buttons that will show other forms. That is if user clicks on a button for bringing on another form, this main form should minimize and whenever user close the other form this form will be back to normal or maximized state.
The code looks like this for my main form that controls other forms (Form1.cs):
namespace MultipleForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/*
* I want to make a public reference to this Form1
* So that when user closes Form2 That is F2 this form will be
* set back to normal or maximized state
*/
private void buttonStartForm2_Click(object sender, EventArgs e)
{
//Declare Form2
Form2 F2 = new Form2();
//Show Form2
F2.Show();
//Minimize Form1
this.WindowState = FormWindowState.Minimized;
}
}
}
And this is Form2.cs
namespace MultipleForms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//FormClosed event handler
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
//Closing Form2
this.Close();
//Now I need a code to make Form1 to recover from minimized
// state to normal or maximized state. how do I should refer to it?
}
}
}
I think I have to do some stuff on form closing event, but how can I initilize the Form1.cs as something public? or I also should initilize Form1.cs in Form2.cs also? or maybe it should be done through Program.cs file:
namespace MultipleForms
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
P.S: it is crtitical that Form2 should be closed, not hided
UPDATE
Using Hans solution, it is the best to use it this way:
private bool isEnabledCmpFrm = false;
public void buttonStartComparisonForm_Click(object sender, EventArgs e)
{
if (!isEnabledCmpFrm)
{
ComparisonForm CmpFrm = new ComparisonForm();
CmpFrm.Show();
CmpFrm.WindowState = FormWindowState.Normal;
this.WindowState = FormWindowState.Minimized;
isEnabledCmpFrm = true;
CmpFrm开发者_如何学Python.FormClosed += delegate
{
this.WindowState = FormWindowState.Normal;
isEnabledCmpFrm = false;
};
}
}
If I understand correctly, you can hook into the Form Closing event.
So in your main form you can do something like:
Form2 form2 = new Form2();
form2.Show();
form2.FormClosing += new FormClosingEventHandler(form2_FormClosing);
void form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.Show();
}
Don't have a form listen to its own events. The code needs to go in Form1. Like this:
Form2 F2 = new Form2();
F2.FormClosed += delegate { this.WindowState = FormWindowState.Normal; };
F2.Show();
this.WindowState = FormWindowState.Minimized;
You need to pass a reference to the main form to the child forms and use the main form's show method to make it visible again when the child form is closing.
精彩评论