Jumat, 17 Desember 2010

An Easy Way to Find All Open Forms in VB.NET

 Dim frm As Form

            For Each frm In My.Application.OpenForms

                If frm Is My.Forms.TheFormINeed Then
                  'do something
end if
 
 Dim frm As Form

    Dim NameToSearch As String

    NameToSearch = "frmMyForm"    ' name of the form I have to look for

    For Each frm in Forms

        If frm.Name = NameToSearch Then

            ' VB has found a form with that name in the collection

            ' it means the form is loaded already

            ' ..... your code here ....

            Exit For

        End If

    Next frm 


PublicClassTheFormThatOpensAnotherForm
InheritsForm
Private_formAsFormThatThisFormOpens=Nothing
PublicSubbutton1_Click(ByValsenderAsObject,ByValeAsEventArgs)
'performsomework
If_formIsNotNothingAndAlsoNot_form.IsDisposedAndAlso_form.VisibleThen
EndIf
EndSub
PublicSubOpenForm()
If_formIsNotNothingAndAlsoNot_form.IsDisposedThen
_form.Show()
Else
_form=NewForm()
_form.Show()
EndIf
EndSub
EndClass



Private Sub button1_Click(sender As Object, e As EventArgs)
 Dim open As Boolean = True
 For Each frm As Form In MdiChildren
  If frm.[GetType]() = GetType(Form1) Then
   open = False
  End If
 Next
 If open Then
  Dim frmOne As New Form1()
  frmOne.MdiParent = Me
  frmOne.Show()
 Else
  MessageBox.Show("Form1 is already open")
 End If
End Sub
Private Sub button2_Click(sender As Object, e As EventArgs)
 Dim open As Boolean = True
 For Each frm As Form In MdiChildren
  If frm.[GetType]() = GetType(Form2) Then
   open = False
  End If
 Next
 If open Then
  Dim frmTwo As New Form2()
  frmTwo.MdiParent = Me
  frmTwo.Show()
 Else
  MessageBox.Show("Form2 is already open")
 End If
End Sub
Private Function CheckIfOpen(ByVal frmName As String) As Boolean
Dim frm 
As Form For Each frm In Me.MdiChildren If frm.Name frmName Then
frm
.Focus()
Return 
True Exit Function End If Next Return False
End 
Function 

Tidak ada komentar:

Posting Komentar