25

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

   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 的更多实例如何检查此表单是否尚未打开

4

8 回答 8

50

你可以这样尝试:

 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
于 2013-03-15T18:23:12.400 回答
12

您可以使用以下代码:

If myForm.IsHandleCreated then
   myForm is open
End If
于 2013-12-20T20:47:59.440 回答
3

作为给出的答案的扩展(谢谢大家),这是一种激活或显示的简单方法:

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
于 2015-01-10T03:37:38.610 回答
3

为了更简单,您可以创建一个公共静态 bool 变量,该变量将告知表单是否打开。在表单加载事件上分配“真”,在关闭事件上分配“假”值。

于 2015-06-05T21:55:29.990 回答
2

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
于 2016-02-12T18:08:31.447 回答
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

然后按要求做节目等?

于 2015-10-21T09:29:27.613 回答
1

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

示例:这将在带有 mdiContainer 的表单和带有 3 个按钮的面板对象中使用它,以显示 3 个窗口表单。

导入系统导入 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) Handles btnRegistro.Click OpenWindowsForm("Registro") End Sub

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

这是示例代码的图像

于 2017-08-04T14:24:29.120 回答
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
于 2016-12-13T17:15:37.873 回答