Results 1 to 5 of 5

Thread: [2005] Open One time

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    [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

  2. #2
    Lively Member
    Join Date
    Nov 2006
    Posts
    116

    Re: [2005] Open One time

    Use this if you are using the mdi container.

    VB Code:
    1. Public Function chkChildForm(ByVal pstrFormName As String, ByVal pParentForm As Form) As Form
    2.         ' get all of the MDI children in an array
    3.         Dim charr() As Form = pParentForm.MdiChildren
    4.         Dim chform As Form
    5.         Dim found As Boolean = False
    6.         If (charr.Length = 0) Then      ' no child form is opened
    7.             Return Nothing
    8.         Else      ' child forms are opened
    9.             For Each chform In charr
    10.                 If (chform.Text = pstrFormName) Then
    11.                     ' one instance of the form is already opened
    12.                     'chform.Activate()
    13.                     Return chform
    14.                     found = True
    15.                     Exit For   ' exit loop
    16.                 Else
    17.                     found = False ' make sure flag is set to
    18.                     ' false if the form is not found
    19.                 End If
    20.             Next
    21.         End If
    22.         Return Nothing
    23.     End Function

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class SingletonForm
    2.  
    3.     ''' <summary>
    4.     ''' The one and only instance of the SingletonForm class.
    5.     ''' </summary>
    6.     Private Shared _instance As SingletonForm
    7.  
    8.     ''' <summary>
    9.     ''' Gets the one and only instance of the SingletonForm class.
    10.     ''' </summary>
    11.     ''' <value>
    12.     ''' A SingletonForm object that represents the only instance of the class.
    13.     ''' </value>
    14.     Public Shared ReadOnly Property Instance() As SingletonForm
    15.         Get
    16.             If _instance Is Nothing OrElse _instance.IsDisposed Then
    17.                 'Either and instance has never been created or the previous instance was disposed.
    18.                 _instance = New SingletonForm
    19.             End If
    20.  
    21.             Return _instance
    22.         End Get
    23.     End Property
    24.  
    25.     ''' <summary>
    26.     ''' Initialises a new instance of the SingletonForm class.
    27.     ''' </summary>
    28.     ''' <remarks>
    29.     ''' The only constructor is declared private so it cannot be invoked from outside the class.
    30.     ''' </remarks>
    31.     Private Sub New()
    32.         ' This call is required by the Windows Form Designer.
    33.         InitializeComponent()
    34.  
    35.         ' Add any initialization after the InitializeComponent() call.
    36.     End Sub
    37.  
    38. 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:
    1. Dim nf As New NormalForm
    2.  
    3. nf.Show()
    you now do this:
    VB Code:
    1. SingletonForm.Instance.Show()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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
    steve

  5. #5

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question 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
  •  



Click Here to Expand Forum to Full Width