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

前端 未结 18 1703
被撕碎了的回忆
被撕碎了的回忆 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:55

    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;
    }
    
    0 讨论(0)
  • 2020-12-05 18:56

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

    ):

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

    I set these three property settings for the form:

    ShowInTaskbar = false
    ShowIcon = false
    WindowState = Minimized
    
    0 讨论(0)
  • 2020-12-05 18:59

    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.

    0 讨论(0)
  • 2020-12-05 19:02

    Try on the VisibleChanged event.

    0 讨论(0)
  • 2020-12-05 19:03

    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;
        }
    
    0 讨论(0)
提交回复
热议问题