Click here to Skip to main content
12,999,837 members (56,477 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 15 Sep 2003

Simple Singleton Forms

, 15 Sep 2003
Rate this:
Please Sign up or sign in to vote.
Simple way to work with singleton forms


When I started with VB.NET a few years ago, one of the things I missed was how to deal with forms where you only want one instance open at the same time. I read a lot of articles concerning Singleton forms etc, which was all complicated and the result was not easy to use. At the end I found a very simple way to do this. Maybe too easy !


The trick is realy very simple. I just make a class with Public Shared variables with as type the Forms itself.

Public Class clsGlobals
#Region "Singleton Forms"
    Public Shared frmStockSelection As frmStockSelection
    Public Shared frmSettings As frmSettings
#End Region
End Class

These public shared variables will be usable eveywhere in your code. You do not create a new instance from this class. Call the variable by using the class name as shown in the next bit of code.

The code to open the Singleton Form is also very simple. The only thing to do is to check if the variable is not nothing, if not create a new instance. You have to use Focus to get your form to the foreground in case an instance was already existing but maybe hidden behind other windows.

If clsGlobals.frmSettings Is Nothing Then
    clsGlobals.frmSettings = New frmSettings
End If

One more thing we have to do to make this work, the variable must be set back to nothing in case we close the form. This we can do in the Dispose sub of the form. After Dispose we can safely set the Form to nothing.

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
        If Not (components Is Nothing) Then
        End If
    End If
    clsGlobals.frmSettings = Nothing
End Sub

Another option is to keep the Form open in memory. This is very handy for Forms, like a little calculator for example where you don't want to start with a clear form every time.
The way to do this is to avoid the close the form and hide it. e.Cancel = True will stop the form from closing.

Private Sub frm_Closing(ByVal sender As Object, _
  ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    e.Cancel = True
End Sub

Maybe the purist readers will not 100% agree with this approach, but for me it works very well. 


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Peter Verijke
Founder Computech bvba
Belgium Belgium
I´m a freelance ICT consultant and Technical Project Manager for the last 15 years already.
Before that, I was a Software Developer for the Pharmaceutical and Petrochemical industry.
I developed a very wide knowledge in the ICT world due to my intrest.
This includes Networking (Lan, Wan), Routing Switching Bridging, etc...
Windows environment, which has no secrets for me.
Developing: To many languages to mension here.
Of course, the latest one on the list is dot net.

You may also be interested in...

Comments and Discussions

GeneralAlso check this out (MDI forms)! Pin
Sire40414-Dec-04 8:56
memberSire40414-Dec-04 8:56 
GeneralSingletons Pin
Adam Byrne22-Sep-03 23:11
memberAdam Byrne22-Sep-03 23:11 
GeneralRe: Singletons Pin
ashleyb18-Jan-04 11:33
memberashleyb18-Jan-04 11:33 
GeneralRe: Singletons Pin
Adam Byrne19-Jan-04 6:31
memberAdam Byrne19-Jan-04 6:31 
Hello Ashley,

How's it going? This behaviour is caused by the chain of events that is started when you close a form. Clicking the little "X" in the top-right corner of a window causes the OnClosing event to be fired, and then when the form is closed, it is Dispose()d of. You don't really want this to happen, because since you are only ever going to have one instance of your window class (because it is a singleton), you shouldn't dispose of it until the end of the application's life.

A way to do this would be to:

  • Create your singleton form by making the constructors private and adding the following static attribute

    <code>public static readonly MyFormClassName Instance = new MyFormClassName()</code>

  • Override the OnClosing method of your form like this:
    <code>protected override void OnClosing(CancelEventArgs e)
        e.Cancel = true;

    so now your form, instead of closing, just hides itself.

  • Now when your application exits, the form should be disposed of automatically, rather than when it is closed. Don't forget that when you show the window again, it will be in the same state as when it was last hidden, i.e. it's not like creating a new instance with clean, fresh, empty controls etc.

    I'm sure there's other ways to achieve this, but this is the first one that came to mind. I hope it helps.

    GeneralRe: Singletons Pin
    ashleyb19-Jan-04 8:18
    memberashleyb19-Jan-04 8:18 
    GeneralRe: Singletons Pin
    Adam Byrne20-Jan-04 0:20
    memberAdam Byrne20-Jan-04 0:20 
    GeneralRe: Singletons Pin
    ashleyb20-Jan-04 9:23
    memberashleyb20-Jan-04 9:23 
    AnswerRe: Singletons Pin
    vdewisme6-May-09 23:11
    membervdewisme6-May-09 23:11 
    GeneralRe: Singletons Pin
    Peter Verijke7-May-09 11:00
    memberPeter Verijke7-May-09 11:00 
    GeneralRe: Singletons Pin
    vdewisme7-May-09 21:46
    membervdewisme7-May-09 21:46 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web02 | 2.8.170624.1 | Last Updated 16 Sep 2003
    Article Copyright 2003 by Peter Verijke
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid