i'm a beginner in MDI form. i created a MDI form with buttons and child forms. when i click the forms continuously, many forms will be opened. how to avoid forms overlapping, in other words, duplicate? thanks
So it's not the fact that you have multiple forms overlapping that is the problem, but the fact that you have multiple forms, right? In that case, your thread title is completely misleading. That's like asking how to stop all your children wearing the same clothes when what you really want is to have no more than one child.
Anyway, while I'm not their biggest fan in general, this is a situation where using default instances can help you out. A default instance is an instance of a form class that the system creates for you. There's only ever one default instance at a time and when one is closed then another is created automatically. Follow the Blog link in my signature and check out my post on the subject.
I use the singleton design approarch for MDI applications.
This code I put on the MDI Form under the button used to launch the child form.
VB.NET:
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
'Show frmNewKingdom form -this is the name of the child form
Dim mdiChild As frmNewKingdom = frmNewKingdom.Instance()
With mdiChild
.MdiParent = Me
.Show()
.Focus()
End With
End Sub
This code I place on my child form that I want to open only one copy.
VB.NET:
Private Shared formInstance As frmNewKingdom 'this is the name of the child form
Public Shared ReadOnly Property Instance() As frmNewKingdom 'this is the name of the child form
'checks to see if the form is open already and opens
'the for if it hasn't already been open.
Get
If formInstance Is Nothing Then
formInstance = New frmNewKingdom 'this is the name of the child form
End If
Return formInstance
End Get
End Property
You will have to set formInstance to nothing when the form closes so that you can open the form again. I hope this helps you. If you have any questions let me know.
This code I place on my child form that I want to open only one copy.
VB.NET:
Private Shared formInstance As frmNewKingdom 'this is the name of the child form
Public Shared ReadOnly Property Instance() As frmNewKingdom 'this is the name of the child form
'checks to see if the form is open already and opens
'the for if it hasn't already been open.
Get
If formInstance Is Nothing Then
formInstance = New frmNewKingdom 'this is the name of the child form
End If
Return formInstance
End Get
End Property
You will have to set formInstance to nothing when the form closes so that you can open the form again. I hope this helps you. If you have any questions let me know.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.