如果我理解正確的話,該代碼是內部的formParent的子form。要使用對當前formParent的引用,您需要在子窗體內傳遞對當前formParent的引用。
所以在formParent,當您啓動子窗體的執行,這樣寫:
Dim f as formChild = new formChild(Me)
f.ShowDialog()
形式子構造
傳遞的參考保存到全局變量formParent1
Dim formParent1 as formParent ' this is the global level reference to the parentForm'
Public Sub formChild(ByVal f as formParent)
formParent1 = f
End Sub
現在,您可以在子表單中使用參考formParent1而無需初始化另一個formParent 例如,假設formParent1包含名爲SearchResult
的公共屬性,那麼當您完成搜索時調用:
Private Sub searchBtn_Click(sender As Object, e As System.EventArgs) Handles SearchButton.Click
Dim searchResult as String
' Here write the code that executes the search and set the value in searchResult variable
' Pass the searchResult variable to the parent form via the global reference.
formParent1.SearchResult = searchResult
End Sub
爲了節省一些擊鍵,可以使用Dim formParent1作爲新的formParent()代替。 – Laoujin
可能重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-it) – Plutonix