JayBeeOH

Singleton Design Pattern for a Modeless Windows Form

Mar 10th, 2015
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.16 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Class Name:     SingletonForm
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   Mar 2015
  16. '
  17. ' Description:    Singleton Design Pattern for a modeless Windows Form.
  18. '
  19. '                 Documentation is at:
  20. '                   Form's Visual Basic .Net code is at http://pastebin.com/gDahYWRx
  21. '                   App's Visual Basic .NET code is at http://pastebin.com/u/jaybeeoh
  22. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  23. '-------------------------------------------------------------------------------------------
  24. ' To Use:
  25. '   Dim aSingletonForm As SingletonForm = SingletonForm.GetInstance()
  26. '   aSingletonForm.Show()
  27. '
  28. '-------------------------------------------------------------------------------------------
  29.  
  30. Public Class SingletonForm
  31.  
  32. #Region " Class Level Variables Declared"
  33.  
  34.     Private Shared myInstance As SingletonForm = Nothing
  35.     Private Shared ReadOnly aLock As New Object
  36.  
  37. #End Region
  38.  
  39. #Region " Private Methods and Functions"
  40.  
  41.     ' Change constructor to a private method.
  42.     Private Sub New()
  43.  
  44.         ' This call is required by the designer.
  45.         InitializeComponent()
  46.  
  47.         ' Add any initialization after the InitializeComponent() call.
  48.     End Sub
  49.  
  50.  
  51.     ' On the FormClosing event hide the form, instead of closing it.
  52.     Private Sub SingletonForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
  53.         Handles Me.FormClosing
  54.  
  55.         ' Cancel the Closing event from closing the form.
  56.         e.Cancel = True
  57.         Me.Hide()
  58.     End Sub
  59.  
  60.  
  61.     ' On the CloseButton Click event hide the form, instead of closing it.
  62.     Private Sub CloseButton_Click(sender As Object, e As EventArgs) _
  63.         Handles CloseButton.Click
  64.  
  65.         Me.Hide()
  66.     End Sub
  67.  
  68. #End Region
  69.  
  70. #Region " Public Methods and Functions"
  71.  
  72.     ' Use factory method to instantiate class.
  73.     Public Shared Function GetInstance() As SingletonForm
  74.  
  75.         ' If constructor has not been evoked, call constructor and return instance.
  76.         ' Otherwise, return the previously created instance.
  77.         If myInstance Is Nothing Then
  78.             ' Tread safe.
  79.             SyncLock aLock
  80.                 If myInstance Is Nothing Then
  81.                     myInstance = New SingletonForm
  82.                 End If
  83.                 Return myInstance
  84.             End SyncLock
  85.         End If
  86.         Return myInstance
  87.     End Function
  88.  
  89. #End Region
  90.  
  91. End Class
Advertisement
Add Comment
Please, Sign In to add comment