views:

2260

answers:

10

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;
        }

    }
}
+3  A: 

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
A: 

Have you tried

this.Hide();

in the form_load or form_activated events

Eoin Campbell
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
+1  A: 

Try on the VisibleChanged event.

David Suarez
I will try that.
tyndall
+1  A: 

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
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
Actually I suggested an approach to Initialize whatever necessary prior to Showing the form.
Nikos Steiakakis
Tried that. That didn't work.
tyndall
A: 

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

Chad Grant
+1  A: 

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;
    }
Chris Persichetti
A: 

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.

Matt Olenik
A: 

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
+5  A: 

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
+1 good idea. Gotcha. Sounds like its the Application.Run that is the culprit.
tyndall
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
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
+1  A: 

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