C#/.NET - WinForms - Instantiate a Form without sh

2019-01-14 15:43发布

问题:

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

    }
}

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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



回答5:

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



回答6:

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(); 

):



回答7:

Try on the VisibleChanged event.



回答8:

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


回答9:

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.



回答10:

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.



回答11:

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


回答12:

Have you tried

this.Hide();

in the form_load or form_activated events



回答13:

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



回答14:

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.



回答15:

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.



回答16:

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.



回答17:

I agree that this can be really maddening, because Winforms usually don't look pretty while they're initializing a bunch of controls or populating a big DataGridView or whatever. Still you do need the window handle to exist before you can do that, creating all the issues that have been mentioned.

Here's something that has worked for me, and you have two choices: You can just hide your main form until it's ready, or you can show some kind of little splash screen to let your user know that you're working on it. Enjoy.

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
    Size mDeferredSize;
    protected override void OnHandleCreated(EventArgs e)
    {
        // Capture the "real" size...
        mDeferredSize = Size;
        // and set it to nothing...
        Size = new Size(0, 0);
        DoSomeUglyInitialization(showOptionalSplash: true);
        Size = mDeferredSize; // ...and now put it back to the original size    
        base.OnHandleCreated(e);
    }
    private void DoSomeUglyInitialization(bool showOptionalSplash)
    {
        MySplash splash = null; 
        if (showOptionalSplash)
        {
            // We have made some borderless form class with a logo or something...
            splash = new MySplash(); 
            splash.Show();
        }
        // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        // Initialization that REQUIRES A HANDLE, e,g,
        // we might be creating a bunch of controls, or 
        // populating a big DataGridView. Whatever it is,
        // we don't want everyone looking at our biz.
        System.Threading.Thread.Sleep(2500); // (Here simulated...)
        // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        splash?.Dispose();      
    }
}


回答18:

I set these three property settings for the form:

ShowInTaskbar = false
ShowIcon = false
WindowState = Minimized