Hide Form on Startup

R

Raj Wall

Hi,
I have an application which, when running, is to be seen only in the system
tray as a right-clickable icon.

However I want the main Form window itself to immediately disappear without
having to be minimized.

I have tried putting
this.Hide();
in the Load method, but that doesn't work (the form still comes up and needs
to be minimized).
I've also tried it (the Hide() code) before and after the
InitializeComponent() call, but that didn't help either.

What is the correct technique to make an application disappear without
having to be automatically minimized?

Thank-you for your help!

Regards,
Raj
 
B

Barry Kelly

Raj Wall said:
I have an application which, when running, is to be seen only in the system
tray as a right-clickable icon.

However I want the main Form window itself to immediately disappear without
having to be minimized.

Probably the easiest way is to not pass it to the Application.Run()
method. Here's a simple windows application which toggles the visibility
of a form with clicking on the icon:

---8<---
using System;
using System.Windows.Forms;
using System.Drawing;

class App
{
static void Main()
{
NotifyIcon icon = new NotifyIcon();
icon.Text = "Foo";
icon.Icon = SystemIcons.Exclamation;
icon.Visible = true;

Form form = new Form();
icon.MouseClick += delegate
{
if (form.Visible)
form.Hide();
else
form.Show();
};

form.FormClosed += delegate
{
icon.Dispose();
Application.Exit();
};

Application.Run();
}
}
--->8---

-- Barry
 
G

Guest

Another solution that can be run in the form constructor/load event:

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

If you can't justify a full form GUI but find a notifyicon sufficient!

-Brandon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top