24

I am changing the Visibility of a Form to false during the load event AND the form still shows itself. What is the right event to tie this.Visible = false; to? I'd like to instantiate the Form1 without showing it.

using System;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Visible = false;
        }

    }
}
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • 4
    I'm baffled by how many people gave answers in this thread without checking the results. Hide(); Visible = false; none of these things work in this scenario. Its not like other forms, its the MAIN form, and it will be shown no matter what you do. People here need to think before giving bogus answers. And BTW, there is no Visible property for the Main Form in Visual Studio 2010. Its simply not there at all. –  Feb 13 '11 at 17:59
  • @Draek, Good point. But you can achieve similar functionality using a combination of properties named ShowInTaskbar and WindowState. I added it as an answer. – Jose Cherian Feb 08 '12 at 07:49

17 Answers17

24

Regardless of how much you try to set the Visible property before the form has been shown, it will pop up. As I understand it, this is because it is the MainForm of the current ApplicationContext. One way to have the form automatically load, but not show at application startup is to alter the Main method. By default, it looks something like this (.NET 2.0 VS2005):

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

If you instead do something like this, the application will start, load your form and run, but the form will not show:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Form1 f = new Form1();
    Application.Run();        

}

I am not entirely sure how this is useful, but I hope you know that ;o)

Update: it seems that you do not need to set the Visible property to false, or supply an ApplicationContext instance (that will be automatically created for you "under the hood"). Shortened the code accordingly.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • +1 good idea. Gotcha. Sounds like its the Application.Run that is the culprit. – BuddyJoe Apr 30 '09 at 14:48
  • Yes, out of curiosity I checked what happens under the hood, and ApplicationContext will set Visible=true on the MainForm, if there is any. If you call Application.Run with a form instance as input, a new ApplicationContext is created that gets the form assigned to the MainForm property. – Fredrik Mörk Apr 30 '09 at 15:51
  • 1
    The trouble with this solution is that closing the form does not close the application. Therefore you must compensate by calling Application.Exit() on the FormClose. Unless ofcourse your application uses more than one form, in which case you'll need to be a bit more intelligent. Luckily mine only uses one form and a system tray icon, so the example above works just fine. – Chris Kemp Sep 15 '10 at 07:24
  • I found a better way to solve this problem which I added as another answer. – Jose Cherian Feb 08 '12 at 07:41
  • I don't know why this answer marked as answer, because on my computer this example doesn't work. Form still visible... – Anatolii Humennyi Jan 02 '14 at 00:43
  • 1
    @AnatoliiGumennyi Can you provide details on how you did? I just retried this (VS2013) and the form does not show for me. – Fredrik Mörk Jan 03 '14 at 17:22
  • Thank you. This finally worked for me after trying many other different examples. – drzounds Jul 29 '16 at 19:18
12

I know this is an old question, but I just stumbled upon it and am pretty surprised no one has mentioned SetVisibleCore:

bool isVisibleCore = false;
protected override void SetVisibleCore(bool value)
{
    base.SetVisibleCore(isVisibleCore);
}

In that code snippet, as long as isVisibleCore remains false, the form will remain invisible. If it's set to false when the form is instantiated, you won't get that brief flash of visibility that you'd get if you set Visible = false in the Shown event.

Nick Spreitzer
  • 10,242
  • 4
  • 35
  • 58
  • this did not created the form – Shimon Doodkin Jan 21 '13 at 10:09
  • when i have created the form outside application.run and did not do form.show(); (witch probably have events to create components at first show). so the components on the form did not created fully. and when i tried to interact with them i got an error component did not initialized. same error was when i have overridden setvisible or set the form to invisible at the beginning. – Shimon Doodkin Jan 26 '13 at 14:19
  • what worked is to create the form and show it with window state minimized and only then hide it and set windows state to normal. (this time the onload event was fired) after trayicon click had to set left top height and width in anyone of them was wrong like -32000 – Shimon Doodkin Jan 26 '13 at 14:27
  • 1
    Warning : this prevents not only the "Shown" event (which you may expect) but also "Load" and maybe others. Be aware of it. They are raised on first call to base.SetVisibleCore(true). – Stéphane Gourichon Aug 27 '13 at 17:34
  • 1
    **NB** this approach will prevent closing the form (and other normal lifecycle events). See http://stackoverflow.com/a/4556649/67392 for a better approach. – Richard Oct 21 '14 at 14:53
  • @Richard, What is **NB**? – Nick Spreitzer Oct 21 '14 at 15:32
  • "NB" == "Nota Bene" which is, essentially, Latin for "important note". For more details: http://en.wikipedia.org/wiki/Nota_bene – Richard Oct 21 '14 at 15:55
  • Ah, okay. :) And yes, that was an important note. Thanks for the comment! – Nick Spreitzer Oct 21 '14 at 16:01
10

It took me some time to find a properly working Solution.

Set the properties named WindowState to Minimized and ShowInTaskbar to False under properties window. Once your form is completly loaded, Call below lines of code.

        this.ShowInTaskbar = true;
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        //this.WindowState = System.Windows.Forms.FormWindowState.Normal;

PS: This solution is Tested on Visual C# 2008 Express Edition

Jose Cherian
  • 7,207
  • 3
  • 36
  • 39
3

How about setting the Opacity property to 0 on design and back to 100 when you want to show the form?

Jules00
  • 31
  • 1
2

a solution i can live with so the form is created and on_load is called on createtion. set WindowState to minimize then on load set visible to false and windowstate to normal

private void Form1_Load(object sender, EventArgs e)
{
        this.Visible = false;
        this.WindowState = FormWindowState.Normal;
}

what did not worked:

the SetVisibleCore override solution did not created the form

as also the

Application {
 Form1 f = new Form1();
 Application.Run(); 

):

Anujith
  • 9,370
  • 6
  • 33
  • 48
Shimon Doodkin
  • 4,310
  • 34
  • 37
2

For a flicker-free Shown solution, also set the form's location off-screen during load:

private Point startuplocation;

private void Form1_Load(object sender, EventArgs e)
{
    this.startuplocation = this.Location;
    this.Location = new Point(-1000, -1000);
}

private void Form1_Shown(object sender, EventArgs e) //fires first time shown
{
    this.Visible = false;
    this.Location = this.startuplocation;
}
Reg Edit
  • 6,719
  • 1
  • 35
  • 46
1

Just create an instance of Form1 and do not call methods to show/display it. But I bet you're doing something wrong.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
1

Try on the VisibleChanged event.

David Suarez
  • 1,090
  • 11
  • 12
1

What I would suggest would be to instantiate the form in an event the precedes the _Show event, such as the constructor, after the IntializeComponent() call.

Nikos Steiakakis
  • 5,605
  • 7
  • 44
  • 54
  • I think perhaps you meant hide rather than instantiate, correct? In any event (no pun intended), you'd have to ensure that wherever you're calling it occurs AFTER the call that makes the form visible, but before it actually BECOMES visible. – Adam Robinson Apr 30 '09 at 13:56
  • Actually I suggested an approach to Initialize whatever necessary prior to Showing the form. – Nikos Steiakakis Apr 30 '09 at 14:01
1

The shown event may give you want you want. Although the form will "flash" for a second before hiding.

private void Form1_Shown(object sender, EventArgs e)
    {
        this.Visible = false;
    }
Crispy
  • 5,557
  • 3
  • 30
  • 35
1

If this is your main form, there may not be a better place then the Shown event. But in that case you will get flicker.

I couldn't find a good place to stop a running main form from showing at least quickly. Even a timer activated in the load event won't do it.

If it is a secondary form just create it but don't show it.

automatic
  • 2,727
  • 3
  • 34
  • 31
0

I set these three property settings for the form:

ShowInTaskbar = false
ShowIcon = false
WindowState = Minimized
0

Have you tried

this.Hide();

in the form_load or form_activated events

Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
  • Just FYI, this doesn't work when used in the form_load event, but does work in the form_activated event, albeit showing the form for a split-second before hiding. – CraigTP Apr 30 '09 at 14:06
0

Set the visibilty on the constructor, after init and then this.Show() later

Chad Grant
  • 44,326
  • 9
  • 65
  • 80
0

InitializeComponent() is setting this.Visible = true, since you specified that the form should be visible in the designer (or it defaulted to that). You should set Visible to false in the designer, and it won't be called by InitializeComponent(). You can then make it visible any time you like.

Matthew Olenik
  • 3,577
  • 1
  • 28
  • 31
0

Having .Visible = false or Hide() in the Load event will cause your form to show briefly, as there is time between when it becomes physically visible and when the Load event gets fired, in spite of the fact that the documentation says the contrary.

Are you calling Show() or ShowDialog() somewhere? I'm not sure if this behavior is still present, but at least in past versions of the framework a call to ShowDialog() did not trigger the Load event, so perhaps that is your issue (though I think calling ShowDialog() then hiding a modal form would be a bad practice!)

If you have to have the handle created (and the handles for controls) for whatever it is you're trying to do, a better idea would be to set the StartLocation to Manual, then set the Position property to an offscreen location. This will create and show the form, while making it invisible to the user.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
0

Yeah the really one elegant way in perspective to your code than your applications visual is to flicker the form by hiding in the constructor/load event.

chaz
  • 568
  • 1
  • 8
  • 22