检查表单是否已打开

时间:2013-03-15 18:15:21

标签: vb.net

我提出这个问题以获得更多知识。我怎么知道表单是否在我的应用程序中打开,以便不再打开它我的意思是在运行时不创建相同表单的实例

   Dim frmCollection As New FormCollection()
    frmCollection = Application.OpenForms()
    If frmCollection.Item("Form2").IsHandleCreated Then
        MsgBox("Yes Opened")
    Else
        Dim f As New Form2()
        With f
            .Text = "form2"
            .Show()
        End With
    End If

如果我多次执行此代码,它将创建Form2格式的更多实例 如何检查此表单是否尚未打开

8 个答案:

答案 0 :(得分:41)

你可以这样试试:

 Imports System.Linq ' need to add 


If Application.OpenForms().OfType(Of Form2).Any Then
  MessageBox.Show("Opened")
Else
  Dim f2 As New Form2
  f2.Text = "form2"
  f2.Show()
End If

答案 1 :(得分:9)

您可以使用以下代码:

If myForm.IsHandleCreated then
   myForm is open
End If

答案 2 :(得分:3)

为了更简单,您可以创建一个公共静态bool变量,它将告诉表单是否打开。在表单加载事件中指定'true',在关闭事件上指定'false'值。

答案 3 :(得分:1)

讨厌成为一种杀戮的快乐,但总有一天会有人试图理解你的代码。

Dim frm as New frmDontknow
Dim frmCollection = System.Windows.Forms.Application.OpenForms
For i As Int16 = 0I To frmCollection.Count - 1I
  If frmCollection.Item(i).Name = frm.Name Then
      frmCollection.Item(i).Activate()
      Exit Sub
  End If
Next i

然后根据需要做节目等?

答案 4 :(得分:1)

检查表单是否已打开,要验证表单是否已打开,我们使用此方法和函数可以从任何表单调用并使用更少的代码。

示例: 这将使用带有mdiContainer的表单和带有3个按钮的面板对象,这些按钮显示3个窗体。

进口系统 Imports System.Reflection

Private Sub OpenWindowsForm(ByVal FormName As String)
    Dim instForm As Form = Application.OpenForms.OfType(Of Form)().Where(Function(frm) frm.Name = FormName).SingleOrDefault()
    If instForm Is Nothing Then
        Dim frm As New Form
        frm = DirectCast(CreateObjectInstance(FormName), Form)
        frm.MdiParent = Me
        Me.Panel1.Controls.Add(frm)
        Me.Panel1.Tag = frm
        frm.Show()
    Else
        instForm.Select()
        instForm.WindowState = FormWindowState.Maximized
        instForm.BringToFront()
    End If
End Sub

Public Function CreateObjectInstance(ByVal objectName As String) As Object
    Dim obj As Object
    Try
        If objectName.LastIndexOf(".") = -1 Then
            objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
        End If

        obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)

    Catch ex As Exception
        obj = Nothing
    End Try
    Return obj

End Function

如何在点击活动中使用     Private Sub btnRegistro_Click(sender as Object,e As EventArgs)处理btnRegistro.Click         OpenWindowsForm( “Registro”)     结束子

Private Sub btnBusqueda_Click(sender As Object, e As EventArgs) Handles btnBusqueda.Click
    OpenWindowsForm("Busqueda")
End Sub

Private Sub btnCalendario_Click_1(sender As Object, e As EventArgs) Handles btnCalendario.Click
    OpenWindowsForm("Calendario")
End Sub

Here is an image of the Sample code

答案 5 :(得分:0)

作为答案的延伸(谢谢大家),这是一个简单的激活或显示方式:

Dim frmCollection = System.Windows.Forms.Application.OpenForms
If frmCollection.OfType(Of Form2).Any Then
    frmCollection.Item("Form2").Activate()
Else
    Dim newForm2 = New Form2
    newForm2.Show()
End If

答案 6 :(得分:0)

另一种由HumbleBeginnings发起的重构方式:

    Dim xChildWindows = Application.OpenForms.OfType(Of frmForm2)
    If xChildWindows.Any Then
        xChildWindows.First().Focus() 'Focus if exists
    Else
        Dim xfrmNew As New frmForm2() 'Open window if doeasn't exists
        xfrmNew.MdiParent = Me
        xfrmNew.Show()
    End If

答案 7 :(得分:0)

你可以试试这个

Dim formText As String
Dim prevText As String

 Private Sub OpenForm(ByVal frm As Windows.Forms.Form)
        formText = frm.Text
        If formText = prevText Then Exit Sub
        CloseForms()
        ' Make it a child of this MDI form before showing it.
        frm.MdiParent = Me
        frm.Show()
        frm.Location = New Point(0, 0)
        prevText = formText
    End Sub

    Private Sub CloseForms()
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
    End Sub

    Private Sub NewToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayablesToolStripMenuItem.Click
            OpenForm(frmPayables)
        End Sub