C#: Hide Main Form

» 17 September 2010 » In C#, Programming »

It’s always been a pain in the neck just to hide the main form in a Windows Form application in C#. There are several ways to do it. By setting the Visible property to false, and using this.Hide(). Bad news is, the two ways I mentioned doesn’t work. Good news is, I got two solutions for this.

The Problem: Why it won’t work?

It’s very hard to brainstorm, explore, and get into trial just to hide the main form. It takes a lot of time and effort. But why does these two, supposed to be working solutions, don’t work? The answer is pretty much complicated, as I am not sure about my answer as well. If you look at your code, there’s definitely nothing wrong with it. The reason why it keeps on showing up, even if we put the right values into these properties, is because the form we are trying to hide is the main form, and it won’t allow us to hide Application.Run(new Form1()) overrides those properties.

The Solutions

I have two solutions for this, one is not so good and the other one seems to be the best. The first solution, is to change the Opacity value of the form from 100 to 0.

this.Opacity = 0;

Or you can just set it at the Properties window in your form designer. Actually, the above solution only works on Windows Operating Systems with Aero enabled (Please correct me if I am wrong here). So if you are using XP or editions of Vista and 7, this won’t work.

The second one, is by modifying Program.cs, the main source file of your C# project. In this solution, the form application will be defined, but it won’t be loaded. And of course, the program stays up and running.

//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace FileIndex
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 mForm = new Form1();
            //Your On Load code here..
            Application.Run();
        }
    }
}

Originally, the Application.Run(); function calls the main form. But in this solution, it doesn’t have to. This will load your application, but will not load the form. The only thing you can’t do here is, place your code at the OnLoad event of the form, since the form will not load. Alternatively, you can place your code between the Form definition, and Application.Run();.

But why declare and define Form1 if we won’t load it? It won’t load, but it doesn’t mean its not functional. You can still use components on that form, like Timer, NotifyIcon, etc. You can also put your stealthy functions in your Form code if you wish.

Dealing with these kind of problems isn’t hard. You just need to think critically, and trace where the problem is coming from.

Tags: , , ,

Trackback URL

  • Pingback: C#: Hide Main Form | Lifecode | Learn Visual Studio

  • Guest

    Wow, thanks a million!

  • Deepfriedmars

    Excellent solution, no wonder it comes up number one on Google!

  • Blood Hazard

    I’m using windows Xp and when i use ‘this.Opacity=0′ the job is done well so ur remark about aero isn’t strong.

    • http://ruel.me Ruel

      Well I guess I’m wrong about that. :) This might be limited by GFX Card capabilities. Are you using a generic GFX card (Intel GMA etc.) or good brands (nVidia, ATI, etc) ?

  • Nishmu

    As a reminder, when using Application.Run method without arguments, the application needs to exit explicitly by using Application.Exit() somewhere in the project (especially the main form).

  • Kyle

    another option is to set the window state to minimized and hide it from the taskbar via the form property

  • Miahfk

    thanks very much, this was very helpful.

  • closl

    thanks!  that was driving me nuts!

  • Terrell James

    I know this is a old post, but this.Hide() and Visible = false works fine for me. I am using Visual Studio 2010. Do you still presently have this problem? 

  • Marft

    And another big big thanks for this simple solution (successfully tried on Win XP with Visual Studio 2010, NET 4.0).

  • Aneesh Apply

    f=good

  • Aneesh

    nice

  • Aneesh Apply

    second method is better, the first method will show it in taskbar

  • Yun

    really really good!!

  • Boxz_gwapa

    past
    ^_^

  • Yok

    Second method really helpful thnx for share :)

  • Simon

    Thank you. Exactly what I was looking for!

  • Blop

    Note :
    Application.Run() works but a call to Process.CloseMainWindow() will not because Application does not have a main windows.

    when windows wants to close, it will try to close gracefully your mainForm. Here no mainform ==> your application will be close via a Kill. not the best way.

  • Nitin

    Thanks alot for this posting.