Single Form Hide on Startup

2019-01-02 17:54发布

I have an application with one form in it, and on the Load method I need to hide the form.

The form will display itself when it has a need to (think along the lines of a outlook 2003 style popup), but I can' figure out how to hide the form on load without something messy.

Any suggestions?

21条回答
旧人旧事旧时光
2楼-- · 2019-01-02 18:01

I have struggled with this issue a lot and the solution is much simpler than i though. I first tried all the suggestions here but then i was not satisfied with the result and investigated it a little more. I found that if I add the:

 this.visible=false;
 /* to the InitializeComponent() code just before the */
 this.Load += new System.EventHandler(this.DebugOnOff_Load);

It is working just fine. but I wanted a more simple solution and it turn out that if you add the:

this.visible=false;
/* to the start of the load event, you get a
simple perfect working solution :) */ 
private void
DebugOnOff_Load(object sender, EventArgs e)
{
this.Visible = false;
}
查看更多
泛滥B
3楼-- · 2019-01-02 18:03

You're going to want to set the window state to minimized, and show in taskbar to false. Then at the end of your forms Load set window state to maximized and show in taskbar to true

    public frmMain()
    {
        Program.MainForm = this;
        InitializeComponent();

        this.WindowState = FormWindowState.Minimized;
        this.ShowInTaskbar = false;
    }

private void frmMain_Load(object sender, EventArgs e)
    {
        //Do heavy things here

        //At the end do this
        this.WindowState = FormWindowState.Maximized;
        this.ShowInTaskbar = true;
    }
查看更多
ら面具成の殇う
4楼-- · 2019-01-02 18:05

Why do it like that at all?

Why not just start like a console app and show the form when necessary? There's nothing but a few references separating a console app from a forms app.

No need in being greedy and taking the memory needed for the form when you may not even need it.

查看更多
无与为乐者.
5楼-- · 2019-01-02 18:06

Launching an app without a form means you're going to have to manage the application startup/shutdown yourself.

Starting the form off invisible is a better option.

查看更多
何处买醉
6楼-- · 2019-01-02 18:06

Based on various suggestions, all I had to do was this:

To hide the form:

Me.Opacity = 0
Me.ShowInTaskbar = false

To show the form:

Me.Opacity = 100
Me.ShowInTaskbar = true
查看更多
心情的温度
7楼-- · 2019-01-02 18:08
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    MainUIForm mainUiForm = new MainUIForm();
    mainUiForm.Visible = false;
    Application.Run();
}
查看更多
登录 后发表回答