views:

3942

answers:

10

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

The form will display itself when it has need to (think along the lines of a outlook 2003 style popup), but i cant for the life of me work out how to hide the form on load without something messy.

Any suggestions?

A: 

Here is a simple approach:
It's in C# (I don't have VB compiler at the moment)

public Form1()
{
    InitializeComponent();
    Hide(); // Also Visible = false can be used
}

private void Form1_Load(object sender, EventArgs e)
{
    Thread.Sleep(10000);
    Show(); // Or visible = true;
}
aku
if you dont add the Show Line in Form1_Load, the form is shown anyway, even if you set visible to false... -1
Eugenio Miró
A: 

In the designer, set the form's Visible property to false. Then avoid calling Show() until you need it.

A better paradigm is to not create an instance of the form until you need it.

deemer
+13  A: 

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, set the Visible property of the form to False using the designer. You can create 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.

Update: You will also want to set the Application.ShutdownMode property to ShutdownMode.OnExplicitShutdown.

Groky
By far and away the best (and one of the simplest) solutions I've seen.All the garbage about setting opacity to zero, having separate Timer instances to hide the form etc have code smell for days.
FerretallicA
A: 

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.

Roger Willcocks
+1  A: 

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.

Matías
A: 

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

A: 

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.

toast
A: 

Extend your main form with this one:

using System.Windows.Forms;

namespace HideWindows
{
    public class HideForm : Form
    {
        public HideForm()
        {
            Opacity = 0;
            ShowInTaskbar = false;
        }

        public new void Show()
        {
            Opacity = 100;
            ShowInTaskbar = true;

            Show(this);
        }
    }
}

For example:

namespace HideWindows
{
    public partial class Form1 : HideForm
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

More info in this article (spanish):

http://codelogik.net/2008/12/30/primer-form-oculto/

+1  A: 

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
Jon
A: 
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MainUIForm mainUiForm = new MainUIForm();
        mainUiForm.Visible = false;
        Application.Run();
    }
kilsek