检查是否在 vb.net 2010 中打开了特定的表单实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17902128/
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
Check if particular instance of form is opened in vb.net 2010
提问by smh
In the posted question: "Check if form is opened" the answer below was posted as correct. However, I would like to know how to check if a particular instance of a form is open before opening it; for example, to check if an editing screen of the same record is being opened again, or a form to add a new record, when another form to do the same thing is open already.
在发布的问题中:“检查表单是否已打开”下面的答案被发布为正确的。但是,我想知道如何在打开表单之前检查表单的特定实例是否已打开;例如,检查是否正在再次打开同一记录的编辑屏幕,或添加新记录的表单,而另一个执行相同操作的表单已打开。
Below is the code posted as the correct answer for the original question. Can it be modified to do what I need? Thanks in advance.
以下是作为原始问题的正确答案发布的代码。可以修改它来做我需要的吗?提前致谢。
If Application.OpenForms().OfType(Of Form2).Any Then
MessageBox.Show ("Opened")
Else
Dim f2 As New Form2
f2.Text = "form2"
f2.Show()
End If
A particular instance would be a form that is editing a particular record from a table. I would also be tracking the status of the edit (whether the form was in edit mode or not) Or, if this form has a child (a form that edits a sub table of this record); the parent form cannot exit until the child is closed.
一个特定的实例是一个正在编辑表中特定记录的表单。我还将跟踪编辑的状态(表单是否处于编辑模式)或者,如果此表单有子表单(编辑此记录的子表的表单);在子窗体关闭之前,父窗体不能退出。
I currently create a tree of open forms, their name, the record they are editing, and the edit status, and their closing is updated in the tree. Answer # 2 at first glance seems like it could handle these situations and there would be no need to have this data structure in the background that needs to be constantly updated whenever an action is taken. It might be possible to make it more general so it could be reused easily from application to application.
我目前创建了一个由打开的表单、它们的名称、它们正在编辑的记录和编辑状态组成的树,并且它们的关闭在树中更新。乍一看,答案 2 似乎可以处理这些情况,并且不需要在后台拥有这种数据结构,只要采取行动就需要不断更新。有可能使它更通用,以便它可以在应用程序之间轻松重用。
采纳答案by competent_tech
Yes, this can easily be modified to do what you are looking for.
是的,这可以很容易地修改来做你正在寻找的东西。
You need to add a public property called Key (or whatever you want) to Form2 and then you can use the ShowOrOpenForm method below to accomplish your goals:
您需要向 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
回答by Neolisk
The parent of your editing screen should store information about its current editing screen. If Nothing, no editing screen is open. If non-Nothing, it's set to the current editing screen. In this case you don't need the headache of dealing with OpenForms.
编辑屏幕的父级应存储有关其当前编辑屏幕的信息。如果没有,则不会打开编辑屏幕。如果非Nothing,则设置为当前编辑屏幕。在这种情况下,您无需为处理OpenForms.
回答by Neil Dunlop
I could find no property of a VB.Net Form that reliably indicates that the Form has been shown and still has not been Disposed. A disappointment, as @smh says. My solution was along the lines of the suggestion by @Hans Passant: "keep a List", though I use a Collection. @Hans Passant suggested another post but it is a C# Post. Here is the code to manage Forms after Showand before Closeor Disposein Visual Basic:
我找不到 VB.Net 表单的任何属性,可以可靠地表明该表单已显示但仍未被处理。正如@smh 所说,令人失望。我的解决方案与@Hans Passant 的建议一致:“保留一个列表”,尽管我使用了一个集合。@Hans Passant 推荐了另一篇文章,但它是一篇 C# 文章。这是在 Visual Basic之后Show和之前Close或Dispose在 Visual Basic 中管理窗体的代码:
In use, I call SetOpenFormwhen creating a New Form, RemoveOpenFormwhen closing a Form (or when Accept on the Form is Clicked). Between those two Events, the Form and all its data can be retrieved using GetOpenForm& the Form's name. It is valid where only one instance of each Form is open at a time.
在使用中,我SetOpenForm在创建新表单、RemoveOpenForm关闭表单时(或单击表单上的接受时)调用。在这两个事件之间,可以使用GetOpenForm& 表单名称检索表单及其所有数据。在每次仅打开每个 Form 的一个实例时有效。
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
回答by Muhammad Siddique
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

