Geeks With Blogs
Mayank Sharma

The scenario:

One main winform that is open all the time when the application starts. It has a menu strip at the top. User clicks on an item in menu strip and new modal window is opened which has more options. When the user clicks a button on this modal form, another modal form needs to be opened and the previous one to be hidden. Then when the user closes the current form, the previous form is displayed again. The main form has to be displayed all the time.

Main form -> Menu item click -> open Modal form 1 -> button or any other item clicked on Model form 1 -> open Model form 2 and hide model form 1 -> user closed model form 2 and model form 1 shows up again.

The weird problem:

Now the problem with this is that if any other window is maximized, be it Windows Exmplorer or the IDE or the Internet explorer, if I hide the second form and display the third form, the first form gets minimized...weird.

Workaround:

Bonnie and John posted very good solutions for this problem.

Solution by Bonnie - Move the second form out of viewable screen and bring it back when the third form is closed.

 MySecondForm oForm = new MySecondForm();  
this.Location = new Point(this.Location.X, this.Location.Y - 5000);  
oForm.ShowDialog();  
this.Location = new Point(this.Location.X, this.Location.Y + 5000);   

Solution by John - Use a timer:

 public partial class MainForm : Form

  {

    public MainForm()

    {

      InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

      Form1 frm1 = new Form1();

      frm1.ShowDialog();

    }

  }

  public partial class Form1 : Form

  {

    public Form1()

    {

      InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

      Form2 Frm2 = new Form2();

      timer1.Start();

      Frm2.ShowDialog();

      this.Show();

    }

    private void timer1_Tick(object sender, EventArgs e)

    {

      timer1.Stop();

      this.Hide();

    }

  }

Heres the link for the thread:

http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/e53437fb-b843-4361-8eff-783fb29ca761#

Posted on Monday, December 22, 2008 6:58 PM Windows Forms | Back to top


Comments on this post: Hiding a modal form and showing a new one minimizes parent window

# Or simply...
Requesting Gravatar...
Just activate the parent form before hiding the child one.

Program.parentForm.Activate();
this.Hide();


Bye.
Left by Luca monteiz Fagioli on Apr 04, 2011 9:25 AM

Your comment:
 (will show your gravatar)


Copyright © bullpit | Powered by: GeeksWithBlogs.net