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

时间:2013-07-27 20:25:36

标签: vb.net

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

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

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似乎可以处理这些情况,并且不需要在后台使用这种数据结构,只要采取行动就需要不断更新。有可能使它更通用,因此可以从应用程序到应用程序轻松地重用它。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

编辑屏幕的父级应存储有关其当前编辑屏幕的信息。如果没有,则不打开编辑屏幕。如果非Nothing,则将其设置为当前编辑屏幕。在这种情况下,您不必担心处理OpenForms

答案 2 :(得分:0)

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

在使用中,我在创建新表单时调用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

答案 3 :(得分:0)

 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
相关问题