The best way to do this is with the Singleton pattern. A Singleton is, by definition, a type of which there can only ever be a single instance. Creating a Singleton class in VB is quite easy:Now you always access the one an only instance of the SingletonForm class via the Instance property. That property is Shared so it is called on the class itself. That means that where normally you would do this:VB Code:
Public Class SingletonForm ''' <summary> ''' The one and only instance of the SingletonForm class. ''' </summary> Private Shared _instance As SingletonForm ''' <summary> ''' Gets the one and only instance of the SingletonForm class. ''' </summary> ''' <value> ''' A SingletonForm object that represents the only instance of the class. ''' </value> Public Shared ReadOnly Property Instance() As SingletonForm Get If _instance Is Nothing OrElse _instance.IsDisposed Then 'Either and instance has never been created or the previous instance was disposed. _instance = New SingletonForm End If Return _instance End Get End Property ''' <summary> ''' Initialises a new instance of the SingletonForm class. ''' </summary> ''' <remarks> ''' The only constructor is declared private so it cannot be invoked from outside the class. ''' </remarks> Private Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. End Sub End Classyou now do this:VB Code:
Dim nf As New NormalForm nf.Show()VB Code:
SingletonForm.Instance.Show()




Reply With Quote