Home Articles VB.Net Articles How to get all instances of a form

How to get all instances of a form

Because in VB.Net you're able to have multiple instances of a form, sometimes you want to be able to get a list of all of the instances of that form that are open, the following function will do this for you.

    ''' <summary>
    ''' This method deals with getting all the instances of a form
    ''' </summary>
    ''' <param name="formName">The name of the form to look for</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function getAllInstancesOfForm(ByVal formName As String) As List(Of Form)
        Dim frm As Form
        Dim i As Integer = 0
        Dim found As New List(Of Form)

        While i < Application.OpenForms.Count
            frm = Application.OpenForms.Item(i)

            If frm.Name.ToLower = formName.ToLower Then
                found.Add(frm)
            End If

            i += 1
        End While

        Return found
    End Function