繁体   English   中英

检查是否在 vb.net 2010 中打开了特定的表单实例

[英]Check if particular instance of form is opened in vb.net 2010

在发布的问题中:“检查表单是否已打开”下面的答案被发布为正确的。 但是,我想知道如何在打开表单之前检查表单的特定实例是否已打开; 例如,检查是否正在再次打开同一记录的编辑屏幕,或添加新记录的表单,而另一个执行相同操作的表单已打开。

以下是作为原始问题的正确答案发布的代码。 可以修改它来做我需要的吗? 提前致谢。

If Application.OpenForms().OfType(Of Form2).Any Then

  MessageBox.Show ("Opened")

Else

  Dim f2 As New Form2

  f2.Text = "form2"

  f2.Show()

End If

一个特定的实例是一个正在编辑表中特定记录的表单。 我还将跟踪编辑的状态(表单是否处于编辑模式)或者,如果此表单有子表单(编辑此记录的子表的表单); 在子窗体关闭之前,父窗体不能退出。

我目前创建了一个由打开的表单、它们的名称、它们正在编辑的记录和编辑状态组成的树,并且它们的关闭在树中更新。 乍一看,答案#2 似乎可以处理这些情况,并且不需要在后台拥有这种数据结构,只要采取行动就需要不断更新。 可能会使其更通用,以便可以在应用程序之间轻松重用。

是的,这可以很容易地修改来做你正在寻找的东西。

您需要向 Form2 添加一个名为 Key(或任何您想要的)的公共属性,然后您可以使用下面的 ShowOrOpenForm 方法来实现您的目标:

Public Sub ShowOrOpenForm(sKey As String)

    If ShowFormForKey(sKey) Then
        MessageBox.Show("Opened")
    Else
        Dim f2 As New Form2

        f2.Key = sKey
        f2.Text = "form2"
        f2.Show()
    End If
End Sub

Private Function ShowFormForKey(sKey As String) As Boolean

    For Each oForm As Form2 In Application.OpenForms().OfType(Of Form2)()
        If oForm.Key = sKey Then
            oForm.Show()
            Return True
        End If
    Next

    Return False
End Function

编辑屏幕的父级应存储有关其当前编辑屏幕的信息。 如果没有,则不会打开编辑屏幕。 如果非无,则设置为当前编辑屏幕。 在这种情况下,您不需要处理OpenForms的麻烦。

我找不到 VB.Net 表单的任何属性,可以可靠地表明该表单已显示但仍未被处理。 正如@smh 所说,令人失望。 我的解决方案与@Hans Passant 的建议一致:“保留一个列表”,尽管我使用了一个集合。 @Hans Passant 建议了另一篇文章,但它是一篇 C# 文章。 下面是在 Visual Basic 中在Show之后和CloseDispose之前管理 Forms 的代码:

在使用中,我在创建新表单时调用SetOpenForm ,在关闭表单时调用RemoveOpenForm (或单击表单上的接受时)。 在这两个事件之间,可以使用GetOpenForm和表单的名称检索表单及其所有数据。 每次仅打开一个表单的一个实例时有效。

Public Shared cOpenForms As Collection  'Place this at the top of your
                                        'set of Forms, e.g. in your MyApp Class.
cOpenForms = New Collection             'Place this in the load sequence of MyApp.

Public Shared Sub SetOpenForm(NamedForm As Form)
    'Saves an Open Form in the Collection of Open Forms
    'Call this every time a New Form is created (if you want to revisit it).
    MyApp.cOpenForms.Add(NamedForm)
End Sub

Public Shared Sub RemoveOpenForm(NamedForm As Form)
    'Removes an Open Form in the Collection of Open Forms, if it is present.  
    'Silently ignores Forms that are not in the Collection.
    'Call this every time a Form is finished with, Closed, Disposed.
    If Not IsNothing(NamedForm) Then
        If MyApp.cOpenForms.Contains(NamedForm.Name) Then
            MyApp.cOpenForms.Remove(NamedForm.Name)
    End If
End Sub
Public Shared Function GetOpenForm(FormName As String) As Form
    'Retrieves a Form if it is in the Collection of Open Forms
    'Call this to retrieve Form FormName; then check for IsNothing.
    For Each f As Form In MyApp.cOpenForms
        If f.Name = FormName Then
            Return f
        End If
    Next
    Return Nothing
End Function
 Dim frm2 As New form2

    If isFormNotOpened(frm2) Then
        frm2.Show()
    End If

Private Function isFormNotOpened(ByVal ThisFrm As Form) As Boolean

    Dim xfrm As Form

    isFormNotOpened = True

    For Each xfrm In Application.OpenForms

        If xfrm.Name = ThisFrm.Name Then

            MsgBox(" !!! Already Opened !!! ", MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation)
            xfrm.Activate()

            isFormNotOpened = False
            Exit Function

        End If
    Next


End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM