How to avoid child forms overlapping in MDI?

gegirl

New member
Joined
Mar 26, 2011
Messages
3
Programming Experience
Beginner
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 :)

*when i click the button continuously.
 
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.
 
In addition you can access the default form instance using the My.Forms object.
 
How to only show one instance of a child form

gegirl,

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.

-Car
 
As said singeltons for all forms is provided by default, you don't need any additional code to utilize that.
 
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.

-Car

You don't have to set it to Nothing. You should do this instead:
VB.NET:
If formInstance Is Nothing OrElse formInstance.IsDisposed Then
 
Back
Top