|
-
Dec 6th, 2006, 01:17 AM
#1
Thread Starter
Just Married
[2005] Open One time
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
-
Dec 6th, 2006, 01:41 AM
#2
Lively Member
Re: [2005] Open One time
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
-
Dec 6th, 2006, 01:44 AM
#3
Re: [2005] Open One time
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:
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 Class
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:
Dim nf As New NormalForm
nf.Show()
you now do this:
VB Code:
SingletonForm.Instance.Show()
-
Dec 6th, 2006, 09:37 AM
#4
Frenzied Member
Re: [2005] Open One time
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
-
Dec 7th, 2006, 02:35 AM
#5
Thread Starter
Just Married
Re: [2005] Open One time
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??
Last edited by shakti5385; Dec 7th, 2006 at 02:39 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|