C#/.NET - WinForms - Instantiate a Form without showing it

前端 未结 18 1702
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 18:29

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

相关标签:
18条回答
  • 2020-12-05 18:41

    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.

    0 讨论(0)
  • 2020-12-05 18:42

    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.

    0 讨论(0)
  • 2020-12-05 18:44

    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

    0 讨论(0)
  • 2020-12-05 18:44

    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.

    0 讨论(0)
  • 2020-12-05 18:46

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

    0 讨论(0)
  • 2020-12-05 18:48

    Have you tried

    this.Hide();
    

    in the form_load or form_activated events

    0 讨论(0)
提交回复
热议问题