vb.net 检查表单是否已打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15439440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:44:47  来源:igfitidea点击:

Check if form is Opened

vb.net

提问by Mohammed Khaled

I give this question for more knowledge. How can I know if the form is Opened in my application or not, in order not to open it again I mean not to create an instance of the same form while it's running

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

   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

if I executes this code many times it will create more instances of the form Form2 How can I check if this form is not already opened

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

回答by LarsTech

You can try it like this:

你可以这样试试:

 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

回答by user3124064

You can use the following code:

您可以使用以下代码:

If myForm.IsHandleCreated then
   myForm is open
End If

回答by Muhammad Zubair ALi

For more simplicity you may create a public static bool variable which will tell whether the form is opened or not. On form load event assign 'true' and on closed event assign 'false' value.

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

回答by Fobbert

Hate to be a kill joy but some day some one is going to try and understand your code.

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

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

Then do the show etc as required?

然后根据需要进行表演等吗?

回答by Dave

ANOTHER refactoring way from the one initiated by HumbleBeginnings:

从 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

回答by HumbleBeginnings

As an extension of the answers given (thank you, all), here's a simple way to activate or show:

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

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

回答by FERIOS

Check if form is Opened, To validate if a form is open we use this method and function to be able to invoke from any form and use less code.

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

Example : This will use it in a form with mdiContainer and a panel object with 3 buttons that shows the 3 windows form.

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

Imports System Imports System.Reflection

导入系统导入 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

How to use in click eventsPrivate Sub btnRegistro_Click(sender As Object, e As EventArgs) Handles btnRegistro.Click OpenWindowsForm("Registro") End Sub

如何在点击事件中使用Private Sub btnRegistro_Click(sender As Object, e As EventArgs) 处理 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

Here is an image of the Sample code

这是示例代码的图像

回答by Jun Gie Cas

you can try this

你可以试试这个

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