Starting an App in Invisible state in VB.Net

invisibleappsampleWindows applications written using the .NET framework do not have a clean way of making the startup form initially invisible or hidden. Setting the Me.Visible property to False in the Load event of the form does not work. In this post, I show you a sample application that starts up invisible and uses a timer to become visible after 10 seconds.

I am assuming the reader knows how to create a basic VB Windows application project in Visual Studio. I have created a project called InvisibleAppSample and added a form called MainForm to it. The basic functionality of keeping the application hidden at startup is achieved by overriding the SetVisibleCore method of the Form class. Add a boolean variable to the form to track visibility.

Private keepInvisible As Boolean

Now, add the following method to the MainForm class to override the SetVisibleCore base method. If you start typing Protected Overrides, Visual Studio 2005 (and upwards, I presume) will show you a list of methods you can override.

    Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
        If keepInvisible Then
        End If
    End Sub

We need to initialize the value of keepInvisible when the class is instantiated, otherwise it will take the default value of False. So, add an instance of the New method for MainForm:

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        ' Add any initialization after the InitializeComponent() call.
        keepInvisible = True
    End Sub

At this point, you can run your app, but the window will remain invisible forever, so it’s of no use. In a production app, you will probably depend on various external events to decide when to make the window visible. For my example, I’m simply using a timer. So, add the timer to the MainForm class:

Private WithEvents showWindowTimer As Timer

Now create the Tick event handler for the timer. VS will help you with the stub if you select ‘showWindowtimer’ from the Object dropdown list (left dropdown list above the code pane) and select ‘Tick’ in the methods list (right dropdown list above the code pane). Mind you, the WithEvents qualifier is necessary for the object to show up in the Object dropdown list.

      Private Sub showWindowTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles showWindowTimer.Tick
        keepInvisible = False
        Me.Visible = True
    End Sub

Hey wait! We never started the timer in the first place! So, add this code to the end of the MainForm constructor (New method):

        showWindowTimer = New Timer()
        showWindowTimer.Interval = 10000

Aaaannnddd we are done! Compile the project and run it (always a good idea to run it in the debugger the first time around, btw). Nothing happens for the 1st 10 seconds, and then I get this window:


If you are too lazy to do all the work from scratch, you can download the VS2k5 solution right here.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *