Talk2Tom11 Posted July 3, 2005 Posted July 3, 2005 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 Quote
Machaira Posted July 3, 2005 Posted July 3, 2005 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. Quote Here's what I'm up to.
PlayKid Posted July 3, 2005 Posted July 3, 2005 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 Quote
jmcilhinney Posted July 4, 2005 Posted July 4, 2005 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 PlayKidAssuming 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 Quote
Simcoder Posted July 5, 2005 Posted July 5, 2005 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=- Quote Whatever thy hand findest to do, do it with all thy heart - Jesus
jmcilhinney Posted July 5, 2005 Posted July 5, 2005 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? Quote
Simcoder Posted July 6, 2005 Posted July 6, 2005 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. Quote Whatever thy hand findest to do, do it with all thy heart - Jesus
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.