Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have 2 forms, form1 activates form2 with showDialog()
C#
Form2 frm2 = new Form2();
            frm2.ShowDialog();


but when i click on minimize on the second form, only form2 minimizes but form1 stays at normal state (can be seen).

The only thing i thought of is to do this:
C#
private void Form2_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                foreach (Form frm in Application.OpenForms)
                {
                    frm.WindowState = FormWindowState.Minimized;
                }
            }
            else if(this.WindowState == FormWindowState.Normal)
            {
                foreach (Form frm in Application.OpenForms)
                {
                    frm.WindowState = FormWindowState.Normal;
                }
            }
        }

And when i click on minimize, its good, all forms minimize, but when i click on the taskbar for the application to get back in normal state, only Form1 shows up..

So how could i fix this?
Posted

Hi,
What you want to do is not possible when the second form is a modal dialog. See an explanation and a work-around (with code) here[^]

Summary:
1) Forcing the main form to minimise actually closes the modal dialog.
2) The work around uses a modeless dialog and simulates modality by temporarily disabling the main form.

Alan.
 
Share this answer
 
Comments
VJ Reddy 15-Apr-12 14:08pm    
Good answer. 5!
Hi,

C#
private void Button_open_frm2_Click(object sender, MouseEventArgs e)
        {
            Form2 form2 = new Form2(this);
            form2.ShowDialog();
        }


Form2.cs:

C#
private Form1 form1 = null;

        public Form2(Form invoke_form2)
        {
            form1 = invoke_form2 as Form1;
            InitializeComponent();

            if (this.WindowState == FormWindowState.Minimized)
            {
                form1.WindowState = FormWindowState.Minimized;
            }
        }



Blind "coded", hope it works for you, give a try if it fits to you.

Best Regards
 
Share this answer
 
v2
Comments
Xonfused 15-Apr-12 10:05am    
but this doesn't minimize form1 also, it still stays the same...
El_Codero 15-Apr-12 10:27am    
passing a reference to form1 is needed. Updated my solution. Regards
Xonfused 15-Apr-12 10:46am    
this new approach also close Form2 after minimizing, and after restoring the application it brings only form1...
El_Codero 15-Apr-12 10:53am    
maybe we talk each past other, what do you mean with Form2 is closing after minimizing? I now tried it in my enviroment,form1 and form2 gets minimized, after clicking on one Form (it doesn't matter if I click on Form1 or Form2 in windows taskbar), both Forms are shown (win 7).
Xonfused 15-Apr-12 11:09am    
Yes, when minimizing like this it also activates the FormClosing event
But i tested and i saw that if i change from Form2.ShowDialog() to only Form2.Show(), Then it works fine.. but though i would like if it is possible with ShowDialog()
Still, thanks for the answers :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900