18

I have the following code in C#:

Form f = new MyForm();
f.Visible = false;
f.Show();
f.Close();

Despite the f.Visible = false, I am seeing a flash of the form appearing and then disappearing. What do I need to do to make this form invisible?

I need to show the form (invisibly) during the splash of my app because doing this removes a cold start delay when showing this form.

Craig Johnston
  • 7,467
  • 16
  • 40
  • 47

5 Answers5

19

If you want to show the form without actually seeing it, you can do this:

  public Form1()
  {
     InitializeComponent();
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
     this.ShowInTaskbar = false;
     this.Load += new EventHandler(Form1_Load);
  }

  void Form1_Load(object sender, EventArgs e)
  {
     this.Size = new Size(0, 0);
  }

If at a later point you want to show it, you can just change everything back. Here is an example after 10 seconds, it shows the form:

  Timer tmr = new Timer();
  public Form1()
  {
     tmr.Interval = 10000;
     tmr.Tick += new EventHandler(tmr_Tick);
     tmr.Start();

     InitializeComponent();
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
     this.ShowInTaskbar = false;
     this.Load += new EventHandler(Form1_Load);
  }

  void tmr_Tick(object sender, EventArgs e)
  {
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
     this.ShowInTaskbar = true;
     this.Size = new Size(300, 300);
  }

  void Form1_Load(object sender, EventArgs e)
  {
     this.Size = new Size(0, 0);
  }
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • 2
    @Craig - Feel free to up vote if its helpful. You'll even get a cool 'Supporter' badge :p – SwDevMan81 Mar 02 '11 at 14:04
  • I voted this as the answer because it provided me what I wanted and it was the simplest (and therefore least bug-prone in my opinion). – Craig Johnston Mar 02 '11 at 14:38
  • 3
    I have the problem that with setting the size to zero the window is invisible on the screen but when hitting ALT + TAB it is listed in the Task Switch dialog although I have set ShowInTaskbar = false! The technique from Kazar using SetVisibleCore is better because the window disappears COMPLETELY everywhere. (See below) – Elmue May 19 '15 at 11:21
  • this will not show in alt + tab : http://stackoverflow.com/questions/2791947/how-to-make-a-program-not-show-up-in-alt-tab-or-on-the-taskbar – Polar Aug 24 '16 at 15:48
  • To hide it from alt+tab replace ``` this.FormBorderStyle = FormBorderStyle.None; ``` with ``` this.WindowState = FormWindowState.Minimized; this.FormBorderStyle = FormBorderStyle.FixedToolWindow; ``` – mat007 May 14 '21 at 09:58
13

By far the simplest way to keep a form invisible is just by not showing it. It is a big deal in Winforms, calling Show() or setting the Visible property to true (same thing) does a lot of things. It is the way the native Windows window gets created. In typical .NET 'lazy' fashion. Any attempt to set Visible back to false (like in OnLoad) will be defeated.

Technically it is possible, you have to override the SetVisibleCore() method. Like this:

    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            this.CreateHandle();
            value = false;   // Prevent window from becoming visible
        }
        base.SetVisibleCore(value);
    }

This ensures that the window doesn't become visible the first time you call Show(). Which is handy when you have NotifyIcon for example, you typically want the icon directly in the notification area and only display a window when the user clicks on the icon. Beware that OnLoad() doesn't run until the window actually becomes visible so move code to the constructor or the override if necessary.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Ummm what is the purpose of having notify icon when my form hasn't even loaded yet.. – Shekhar_Pro Mar 02 '11 at 13:58
  • By far most common example is like.. when you start visual studio it first shows splash screen then when it completely loaded IDE it adds the Notify icon for Customer Improvement program.. – Shekhar_Pro Mar 02 '11 at 14:00
  • @Shekhar - None of the programs that have icons in the notification area on my machine right now display a window first when they get started. They do however have a hidden window, required to make the icon respond to clicks. – Hans Passant Mar 02 '11 at 14:06
  • 1
    @Shekhar - more common examples are the battery meter, volume control, Windows Update status, Wifi connection status, action center. – Hans Passant Mar 02 '11 at 14:07
  • +1 nice examples.. but aren't some of them services instead of winforms ? – Shekhar_Pro Mar 02 '11 at 14:11
  • 1
    @Shekhar - no, a service cannot interact with the desktop anymore. – Hans Passant Mar 02 '11 at 14:23
  • ok.. i didn't knew that... thanx for the info.. (i have already upvoted your answer) :) – Shekhar_Pro Mar 02 '11 at 14:25
9

Simply Because f.Show() is making the form Visible again and f.Close() is Closing it... so the flash.

If you see the MSDN doc for Form.Show() method it clearly mentions that:

Showing the control is equivalent to setting the Visible property to true. After the Show method is called, the Visible property returns a value of true until the Hide method is called.


If you don't want the flash just don't show the Form at all.
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
4

You need to edit the MyForm class, and add the following override method:

protected override void SetVisibleCore(bool value)
{
    //just override here, make sure that the form will never become visible
    if (!IsHandleCreated) CreateHandle();
    value = false;
    base.SetVisibleCore(value);
}

You should ask yourself if this is really necessary however - probably indicative of poor design really.

Edit: It's pretty rare that you need to do this, the only situation I've used it is when I needed a form I could place a COM component on (since the COM component needed a Window handle), and I had to run Application.Run(formInstance) which calls the Show method of the form. By forcing the form to always be invisible, you get a window handle and a message loop without seeing anything on screen.

Alistair Evans
  • 36,057
  • 7
  • 42
  • 54
  • Will this allow me to show the form at a later time, or will it always be invisible? – Craig Johnston Mar 02 '11 at 13:52
  • I'd put a variable in your form to enable this behaviour, and then you create your form, decide if you want that instance to be allowed to become visible by setting that variable, and then Show the form. – Alistair Evans Mar 02 '11 at 13:56
  • 1
    It is not so rare doing this. Sometimes you need an invisible toplevel window just to receive windows messages from another process. – Elmue May 19 '15 at 11:24
  • How to handle the case you only want a notify icon + context menu (but no main form) ? – tigrou Oct 23 '22 at 20:44
0

I've created helper method that will to show invisible form and subsequent Show calls will show window as usually:

public static class FormHelper
{
    public static void ShowInvisible(this Form form)
    {
        // saving original settings
        bool needToShowInTaskbar = form.ShowInTaskbar;
        WindowState initialWindowState = form.WindowState;

        // making form invisible
        form.ShowInTaskbar = false;
        form.WindowState = FormWindowState.Minimized;

        // showing and hiding form
        form.Show();
        form.Hide();

        // restoring original settings
        form.ShowInTaskbar = needToShowInTaskbar;
        form.WindowState = initialWindowState;
    }
}

So form will be rendered (and Load event will trigger) without any blink.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90