[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Only One Instance of MDI Child Form

In my MDI parent, I can launch many different forms as many times as I like (hence, MDI). However, for one form there should only be one instance. How do I do that? I tried a couple of the solutions (via search on EE), but they did not work. Any ideas?
0
GivenRandy
Asked:
GivenRandy
  • 2
  • 2
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one approach:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not loadMDIChild("Form2") Then
            Dim f As New Form2
            f.Name = "Form2"
            f.MdiParent = Me
            f.Show()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Not loadMDIChild("Form3") Then
            Dim f As New Form3
            f.Name = "Form3"
            f.MdiParent = Me
            f.Show()
        End If
    End Sub

    Private Function loadMDIChild(ByVal childName As String) As Boolean
        Dim f As Form
        For Each f In Me.MdiChildren
            If f.Name.Equals(childName) Then
                If f.WindowState = FormWindowState.Minimized Then
                    f.WindowState = FormWindowState.Normal
                End If
                f.Focus()
                Return True
            End If
        Next
        Return False
    End Function

End Class
0
 
RonaldBiemansCommented:
If it is just for one kind of form you could also just set a boolean value in your parent and check that
0
 
GivenRandyAuthor Commented:
That is my fallback. With all the functionality in the .NET Framework, I figured there must be a more straight-forward way that I was overlooking.
0
 
RonaldBiemansCommented:
Well I haven't found another way then ones Idle_mind and I suggested.
0
 
GivenRandyAuthor Commented:
Thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now