Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2015. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Class Name: SingletonForm
- '
- ' Author: Joseph L. Bolen
- ' Date Created: Mar 2015
- '
- ' Description: Singleton Design Pattern for a modeless Windows Form.
- '
- ' Documentation is at:
- ' Form's Visual Basic .Net code is at http://pastebin.com/gDahYWRx
- ' App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- ' To Use:
- ' Dim aSingletonForm As SingletonForm = SingletonForm.GetInstance()
- ' aSingletonForm.Show()
- '
- '-------------------------------------------------------------------------------------------
- Public Class SingletonForm
- #Region " Class Level Variables Declared"
- Private Shared myInstance As SingletonForm = Nothing
- Private Shared ReadOnly aLock As New Object
- #End Region
- #Region " Private Methods and Functions"
- ' Change constructor to a private method.
- Private Sub New()
- ' This call is required by the designer.
- InitializeComponent()
- ' Add any initialization after the InitializeComponent() call.
- End Sub
- ' On the FormClosing event hide the form, instead of closing it.
- Private Sub SingletonForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
- Handles Me.FormClosing
- ' Cancel the Closing event from closing the form.
- e.Cancel = True
- Me.Hide()
- End Sub
- ' On the CloseButton Click event hide the form, instead of closing it.
- Private Sub CloseButton_Click(sender As Object, e As EventArgs) _
- Handles CloseButton.Click
- Me.Hide()
- End Sub
- #End Region
- #Region " Public Methods and Functions"
- ' Use factory method to instantiate class.
- Public Shared Function GetInstance() As SingletonForm
- ' If constructor has not been evoked, call constructor and return instance.
- ' Otherwise, return the previously created instance.
- If myInstance Is Nothing Then
- ' Tread safe.
- SyncLock aLock
- If myInstance Is Nothing Then
- myInstance = New SingletonForm
- End If
- Return myInstance
- End SyncLock
- End If
- Return myInstance
- End Function
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment