Home Articles VB.Net Articles Check whether an instance of a form is open

Check whether an instance of a form is open

In this VB.Net Tutorial/Code Example I'll be showing you how to check to see whether there is an instance of a form open in your application.

    ''' <summary>
    ''' This method deals with checking to see whether a particular form is open
    ''' </summary>
    ''' <param name="formName">The name of the form that you are looking for</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function isFormOpen(ByVal formName As String) As Boolean
        Dim frm As Form
        Dim i As Integer = 0
        Dim found As Boolean = False

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

            If frm.Name.ToLower = formName.ToLower Then
                found = True
                Exit While
            End If

            i += 1
        End While

        Return found
    End Function