Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimize a winforms app when there is at least one modal window opened

Tags:

c#

.net

winforms

I have two form classes (Form1 and Form2) in a Winforms App.

Form1 is like this:

Form1 with a button on it

Form2 is like this (ShowInTaskbar = false):

Form2 with an OK and Cancel buttons

And this code on Form1:

    Form2 someForm = new Form2();
    private void btOpenAnotherWindow_Click(object sender, EventArgs e)
    {
        if (someForm.ShowDialog(this) == DialogResult.OK)
            MessageBox.Show("OK!!!");
        else
            MessageBox.Show("Not OK.");
    }

That is, a window with a button which opens modally another windows when clicked, and waits for the user to close the second window (by clicking the "OK" or "Cancel" button). And depending how it was closed, do alternating actions (represented here by the MessageBox.Show() calls).

I need:

  1. The user can use only one window at a time. (Modal forms, That's why I used ShowDialog() instead of Show())
  2. When the form closes, do something depending on how the form was closed (the "if (someForm.ShowDialog(this)...")
  3. To be able (as a user) to minimize the WHOLE APP.
  4. To be able to "unminimize" the app to the former state correctly.
  5. The program to respond to WIN+M (minimize all) keys combination.

the above example fails in two ways:

  1. (need 5) Doesn't respond to WIN+M
  2. (need 3) The app seems to minimize when the Minimize title bar button is clicked, but it is an illusion because the main form (Form1) does not minimize and it is in fact just hidden behind the other opened windows. Only running the example with an empty desktop shows what really happens. Pics follow:

Before Minimize button is clicked:

Empty desktop with the app running

After: Empty desktop with the Form2 (someForm) minimized

Note:

  1. The Main form is not minimized
  2. The Form2 is in the left botton corner of the screen.

Form2 is a full blown window (not a dialog window per se) and I need the user to interact with it only until it is closed and I also need the user to be able to miminize the whole app in case he needs it.

It is a shame I can't post here the real forms, It would be clearer than these mock-ups.

I need a solution that works with many levels of modal windows (not only two as this example shows). Any suggestions?

like image 319
Yanko Hernández Alvarez Avatar asked Jan 26 '11 18:01

Yanko Hernández Alvarez


1 Answers

I may need a little more information about what you're trying to do here. I have a simple form (Form1) with a button on it, which calls this code:

  private void button1_Click(object sender, EventArgs e)
  {
     Form1 form2 = new Form1();
     form2.ShowDialog();
  }

When I click the button, I get a second instance of the same form, but it's modal. I still have the option to minimize this second modal form (I obviously can't interact with the first form), and when I do minimize the second form it does minimize the entire application (both forms). Now obviously you're asking the question, so I don't think I'm understanding you. =) What about this scenario do you wish to change?

  • C
like image 58
Chris Barlow Avatar answered Oct 15 '22 03:10

Chris Barlow