Parent child forms

Talk2Tom11

Centurion
Joined
Feb 22, 2004
Messages
144
Location
Westchester, NY
Hello I have a question about parent child forms. I have form1 and form2. Form1 being the parent and 2 being the child. When I click a button in form1 it opens form2 in it. my problem is that if I click that button again it opens another instance of form2. What i want to do it only allow one instance of form2 to be opened at a time. my code for the button is below.

Visual Basic:
 Dim NewMDIChild As New Form2()
   'Set the Parent Form of the Child window.
   NewMDIChild.MdiParent = Me
   'Display the new form.
   NewMDIChild.Show()
End Sub
 
You can make the child a member of the parent. Move the declaration for NewMDIChild to the Declarations section of the form, set the MdiParent property in Form1's constructor or Load event and just show the child when the button is clicked instead of creating a new instance every time.
 
Hi Machaira,

I have tried your method by calling the form only when a button is clicked.
But it still didn't solve the problem, coz when I click the button again, a new form is created, how do I stop allowing creating a new form, like when I click the same button, the form that just called comes up again instead of creating a new form?

Thanks

PlayKid
 
PlayKid said:
Hi Machaira,

I have tried your method by calling the form only when a button is clicked.
But it still didn't solve the problem, coz when I click the button again, a new form is created, how do I stop allowing creating a new form, like when I click the same button, the form that just called comes up again instead of creating a new form?

Thanks

PlayKid
Assuming that myChild is a member of the parent form, use code like this:
Visual Basic:
If Me.myChild Is Nothing OrElse Me.myChild.IsDisposed Then
    'Either the child never existed or has been closed so create a new one.
    Me.myChild = New Form2
    Me.myChild.Show()
Else
    'The child already exists so activate it.
    Me.myChild.Activate()
End If
 
Or You Can Check For Instances Of The Form And Just Close It

Visual Basic:
        Dim frm As Form

        'Loop Through Child Forms To See If A Child Form Is Already Open
        'If So, Close The Form

            For Each frm In Me.MdiChildren
                If TypeOf frm Is frmChild Then
                frm.Close()
            Next frm

-=Simcoder=-
 
Simcoder said:
Or You Can Check For Instances Of The Form And Just Close It

Visual Basic:
        Dim frm As Form

        'Loop Through Child Forms To See If A Child Form Is Already Open
        'If So, Close The Form

            For Each frm In Me.MdiChildren
                If TypeOf frm Is frmChild Then
                frm.Close()
            Next frm

-=Simcoder=-
Why would you close the form you just went to the trouble of finding, only to show another?
 
Well thats up to him what he wants to do with the form once he finds it, But I wrote a few applications where it was necessary to make sure all instances of a particular form were closed out before opening a new one. For example, in one project, I had a connection form that connected a user to a sql database. If the user tried to reconnect for whatever reasons, I would close out the connection form then open up a new one so the screen wouldn't get cluttered with connection forms. But it really all depends on what he's trying to do. Close it, activate it, whatever, just do something with the form.
 
Back
Top