Hi all
how to open any form only single time mean if the form is already open then it not open the form again and post the message
Printable View
Hi all
how to open any form only single time mean if the form is already open then it not open the form again and post the message
Use this if you are using the mdi container.
VB Code:
Public Function chkChildForm(ByVal pstrFormName As String, ByVal pParentForm As Form) As Form ' get all of the MDI children in an array Dim charr() As Form = pParentForm.MdiChildren Dim chform As Form Dim found As Boolean = False If (charr.Length = 0) Then ' no child form is opened Return Nothing Else ' child forms are opened For Each chform In charr If (chform.Text = pstrFormName) Then ' one instance of the form is already opened 'chform.Activate() Return chform found = True Exit For ' exit loop Else found = False ' make sure flag is set to ' false if the form is not found End If Next End If Return Nothing End Function
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()
Hi Shakti,
Jmichinney is right, singleton forms is the best solution for this.
Here is article about using it was well. That is what I use.
http://www.codeproject.com/cs/design/singletonforms.asp
Hope it helps,
Steve
Stave you example in the C # but thanks u also.
Thank JMC for the great help again; it is the nice solution using the concept of oops.
Really you have a good oops concept.
Thanks.
But here is need to put this code again and again on every form so same coding is repeating , One solution is that make a use control of the form but any other way??
Can we put this code in other class or not??