Saeid Yazdani Saeid Yazdani - 2 months ago 8
C# Question

Minimizing and restoring a form while showing another form

I have 2 forms, one of them is being showed through clicking a button in the first one. I want it such that when the button for showing 2nd form is clicked, first form gets minimized, and the second form opens normally. And when the 2nd form is being closed, the first window should become un-minimized and be placed where it was before.

User can override this and just click on the first form in quick luanch area and bring up the first form if he/she wants, so I dont want the 2nd from to me of Dialog kind.

I am trying the code below, but when I click on the button, the 1st form gets minimized, and also the second form is being minimized too! so I have to manually un-minimize 2nd form!

private void button1_Click(object sender, EventArgs e)
{
if (DebugForm != null)
return;

DebugForm = new DebugForm();
DebugForm.Closed += delegate
{
WindowState = FormWindowState.Normal;
DebugForm = null;
};

WindowState = FormWindowState.Minimized;
DebugForm.Show();
DebugForm.WindowState = FormWindowState.Normal;
DebugForm.BringToFront();
}

Answer

It seems to minimize both windows when there is another window on your screen. Minimize all windows except your application. You will see that it works. Keep it working along with other windows like this:

Change this:

WindowState = FormWindowState.Minimized;
DebugForm.Show();
DebugForm.WindowState = FormWindowState.Normal;
DebugForm.BringToFront();

to this:

DebugForm.Show();
WindowState = FormWindowState.Minimized;

The lines I removed weren't really necessary. The important thing is the order of statements to coordinate with the Z order of the parent form before minimizing it.