2013-07-27 27 views
3

在發佈的問題中:「檢查表單是否打開」以下答案張貼爲正確。但是,我想知道如何在打開表單之前檢查表單的特定實例是否已打開;例如,檢查是否再次打開同一記錄的編輯屏幕,或者添加新記錄的表單是否已經打開。檢查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似乎可以處理這些情況,並且不需要在後臺執行此數據結構,只要採取行動就需要不斷更新。有可能使它更通用,以便從應用程序到應用程序很容易重用。

+0

你是什麼意思的特定實例?你如何區分一個或另一個實例? –

+0

在你承諾這樣做之前,一定要閱讀[這個問題](http://stackoverflow.com/questions/3751554/application-openforms-count-0-always)。只需將表單的實例存儲在List中,以便稍後您不需要幫助就可以找回它。 –

+0

感謝你報告這個 – smh

回答

2

是的,這可以很容易地修改,以做你正在尋找。

您需要添加一個名爲密鑰的公共財產(或任何你想)到窗體2,然後你可以使用下面的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 
+0

這是一個很好的開始,我認爲,開發一種跟蹤表單的方法。不幸的是,從vb6移動到.net刪除了跟蹤表單打開的能力。我還沒有找到任何與之相對應的東西,但我希望找到比我現在所做的更簡單的事情。 – smh

0

您編輯屏幕的家長應保存有關其當前編輯屏幕的信息。如果沒有,則不會打開編輯屏幕。如果爲非Nothing,則設置爲當前編輯屏幕。在這種情況下,您不需要處理OpenForms的頭痛問題。

+0

是的,謝謝。如果檢查表單是否打開,將會阻止錯誤。 – smh

+0

@smh:如果我的回答很有幫助,請不要忘記註冊。 – Neolisk

0

我無法找到VB.Net窗體的任何屬性,這些屬性可靠地表明窗體已經顯示並且仍然沒有被處置。 @smh說,令人失望。我的解決方案符合@Hans Passant的建議:「保留列表」,儘管我使用了Collection。 @Hans Passant建議另一篇文章,但它是C#Post。這裏是Show後和之前在Visual Basic CloseDispose管理表單代碼:

在使用中,我稱之爲SetOpenForm關閉表單(或當窗體上接受被點擊)當創建一個新的形式,RemoveOpenForm時。在這兩個事件之間,可以使用表單的名稱檢索表單及其所有數據。只有每個表單的一個實例一次打開時纔有效。

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 
相關問題