Jason94
Jason94

Reputation: 13620

hiding my window from the start?

I have a form (Form1, the main form) that I wish to hide at the very start.

I want to do this because I've got a notifyIcon that, by clicking, sets visible true or false for the form, but I'm not able to set the visibility for the form at the very start.

Thanks

Upvotes: 3

Views: 110

Answers (2)

driis
driis

Reputation: 164341

You don't have to display a Form on startup. You can simply use the Application.Run() overload that does not take a Form instance. That will start the Windows application message pump without displaying any UI.

Prior to doing Application.Run(), you can setup your NotifyIcon to be displayed. In effect, this means that the NotifyIcon will be visible after startup, and when the user interacts with the icon, you can open up the Form.

Here is a minimal working C# program that does that: Display a NotifyIcon at startup, and displaying a Form when the user clicks it:

using System;
using System.Drawing;
using System.Windows.Forms;

class Program
{
    static void Main()
    {
        NotifyIcon icon = new NotifyIcon();
        icon.Icon = new Icon("C:\\Windows\\System32\\PerfCenterCpl.ico");
        icon.Visible = true;
        icon.Click += (s,e) => new Form().Show();
        icon.DoubleClick += (s,e) => Application.Exit();
        Application.Run();
    }
}

Upvotes: 5

Deleted
Deleted

Reputation: 4998

I've got an application that does exactly that. To solve it, do this in your Form's OnLoad and it won't appear straight away or in the task bar:

protected override void OnLoad(EventArgs e)
{
    Visible = false;
    ShowInTaskbar = false;
    base.OnLoad(e);
}

Upvotes: 3

Related Questions