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

I'm coming at this from C#, but should be very similar in vb.net.

In your main program file, in the Main method, you will have something like:

Application.Run(new MainForm());

This creates a new main form and limits the lifetime of the application to the lifetime of the main form.

However, if you remove the parameter to Application.Run(), then the application will be started with no form shown and you will be free to show and hide forms as much as you like.

Rather than hiding the form in the Load method, initialize the form before calling Application.Run(). I'm assuming the form will have a NotifyIcon on it to display an icon in the task bar - this can be displayed even if the form itself is not yet visible. Calling Form.Show() or Form.Hide() from handlers of NotifyIcon events will show and hide the form respectively.

查看更多
十年一品温如言
3楼-- · 2019-01-02 18:17

I use this:

private void MainForm_Load(object sender, EventArgs e)
{
    if (Settings.Instance.HideAtStartup)
    {
        BeginInvoke(new MethodInvoker(delegate
        {
            Hide();
        }));
    }
}

Obviously you have to change the if condition with yours.

查看更多
怪性笑人.
4楼-- · 2019-01-02 18:18

This example supports total invisibility as well as only NotifyIcon in the System tray and no clicks and much more.

More here: http://code.msdn.microsoft.com/TheNotifyIconExample

查看更多
姐姐魅力值爆表
5楼-- · 2019-01-02 18:19

Override OnVisibleChanged in Form

protected override void OnVisibleChanged(EventArgs e)
{
    this.Visible = false;

    base.OnVisibleChanged(e);
}

You can add trigger if you may need to show it at some point

public partial class MainForm : Form
{
public bool hideForm = true;
...
public MainForm (bool hideForm)
    {
        this.hideForm = hideForm;
        InitializeComponent();
    }
...
protected override void OnVisibleChanged(EventArgs e)
    {
        if (this.hideForm)
            this.Visible = false;

        base.OnVisibleChanged(e);
    }
...
}
查看更多
梦醉为红颜
6楼-- · 2019-01-02 18:25

At form construction time (Designer, program Main, or Form constructor, depending on your goals),

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

When you need to show the form, presumably on event from your NotifyIcon, reverse as necessary,

 if (!this.ShowInTaskbar)
    this.ShowInTaskbar = true;

 if (this.WindowState == FormWindowState.Minimized)
    this.WindowState = FormWindowState.Normal;

Successive show/hide events can more simply use the Form's Visible property or Show/Hide methods.

查看更多
只靠听说
7楼-- · 2019-01-02 18:25

This works perfectly for me:

[STAThread]
    static void Main()
    {
        try
        {
            frmBase frm = new frmBase();               
            Application.Run();
        }

When I launch the project, everything was hidden including in the taskbar unless I need to show it..

查看更多
登录 后发表回答