How can I hide form1 while Application.Run(form1) is executing?

14,483

Solution 1

Just override the OnVisibleChanged method and change the visibility of the form in there, something like this:

protected override void OnVisibleChanged(EventArgs e)
{
    base.OnVisibleChanged(e);
    this.Visible = false;
}

And that's it! Simple and clean.

Solution 2

See this : Use the ApplicationContext Class to Fully Encapsulate Splash Screen Functionality

Its basically talks about how you would show a splash form first, then when you've finished loading, how to call your main form. (If you hide the main form while doing something in the background, do consider a splash screen)

Quote from Introduction: This isn't an example on how to create a splash screen for your app. This article explains a clean way to encapsulate splash screen functionality into an inherited ApplicationContext class. This article also shows in detail what happens behind the scenes when a WinForm app starts.

Solution 3

You can override SetVisibleCore in your Form1 class, to have the Form1 instance hidden on startup. So here is an example which will hide and keep the form hidden, of course you should add some logic that will decide when the form should actually be allowed to become visible.

public partial class Form1 : Form 
{ 
  public Form1() 
  { 
    InitializeComponent(); 
  } 

  protected override void SetVisibleCore(bool value) 
  {       
    // Quick and dirty to keep the main window invisible
    // you can put some logic here to decide when to use the
    // incomming value and when to force it to be false as I 
    // am showing here.       
    base.SetVisibleCore(false); 
  } 
} 

Here is a simple example albeit contrived. Form1 starts invisible and shows Form2, when Form2 is closed it allows Form1 to become visible and shows the form.

using System;
using System.Windows.Forms;

namespace HideMainWinForm
{
  public partial class Form1 : Form
  {
    // Initially the main form cannot show
    private bool _canShow = false; 

    public Form1()
    {
      InitializeComponent();

      Form2 frm = new Form2();
      frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
      frm.Show();      
    }

    void frm_FormClosed(object sender, FormClosedEventArgs e)
    {
      // Once Form2 is closed we now allow the main form to
      // become visible.
      _canShow = true;
      this.Show();
    }

    protected override void SetVisibleCore(bool value)
    {
      base.SetVisibleCore(_canShow && value);
    }
  }
}
Share:
14,483
Alex
Author by

Alex

Updated on July 31, 2022

Comments

  • Alex
    Alex almost 2 years


    I have a form1 that I run with Application.Run.

    I want to hide this form (I need it hidden because I run some things in the background, so they must execute) and open another form for a log-in.

    The way I am trying this is by executing in my form1 constructor the command this.Hide(); and if log in is successful, show my form1, but it doesn't seem to work. Any ideas?