Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

 Dim NewMDIChild As New Form2()
  'Set the Parent Form of the Child window.
  NewMDIChild.MdiParent = Me
  'Display the new form.
  NewMDIChild.Show()
End Sub

Posted
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.
Here's what I'm up to.
Posted

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

Posted
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:
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

Posted

Or You Can Check For Instances Of The Form And Just Close It

 

       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=-

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted
Or You Can Check For Instances Of The Form And Just Close It

 

       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?
Posted
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.
Whatever thy hand findest to do, do it with all thy heart - Jesus

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...