user961627
user961627

Reputation: 12747

How to ensure only one instance of a child form is created by a parent mdi form in VB.NET

I have a button on a parent form that can create new Child forms.

However, I don't want more than one instance of each form to be created. I tried putting a public boolean on the parent MDI form: Dim ChildForm As Boolean = False

And at the point where the child form is created: ChildFormThere = True

And in the child form's "Leave" event, I thought I could do this:

Me.MdiParent.ChildFormThere = False

But it doesn't recognize the ChildFormThere variable... How can this be done then?

Upvotes: 0

Views: 2117

Answers (2)

PatFromCanada
PatFromCanada

Reputation: 2788

How about something like this. The idea is that if the form has already been created you switch to it, otherwise create one. This assumes you are setting mdiParent correctly when creating the child forms. This code would need to be run on the mdiParent, or have a reference to it to access the MdiChildren property.

For Each f In Me.MdiChildren
    If TypeOf (f) Is Form1 Then
        f.Show()
        Exit Sub
    End If
Next

Dim frm As New Form1
frm.Show()

Upvotes: 2

Origin
Origin

Reputation: 2023

Perhaps instead of:

dim ChildFormThere as Boolean = False ' Or True

You could do:

dim ChildForm as New ChildFormClass

' On Create Button Click:
ChildForm.Visible = True

This way it's always the same instance, so you simple have to manage if it is visible or not.

Upvotes: -1

Related Questions