[英]VB.Net passing datagridview row from one form to another form that is already open
我正在嘗試開發一個搜索窗口,允許用戶填寫條件,然后單擊搜索按鈕。 單擊搜索按鈕后,結果將返回到DataGridView
。 單擊DataGridView
的結果時,我想將行號傳遞給另一個已經打開的表單。
我知道我可以通過創建表單的新實例來傳遞結果。 我使用以下方法將變量傳遞給搜索窗口:
Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
Dim ST = New Search_Tool(DGV_Ongoing.DataSource, AgencyOptions, EthnicityOptions, EmploymentTypeOptions, EmploymentStatusOptions,
CategoryOptions, OutcomeOptions)
ST.Show()
End Sub
但是,我無法調用要將行值傳遞給的表單,因為它已經打開了(它是主表單)。 有辦法解決嗎? 我正在努力尋找在傳遞數據時沒有調用該表單的任何信息。
我無法引用Main_form.Main_DGV.CurrentCell
因為Visual Studio表示需要這樣做才能使用對象引用。
提前致謝!
我建議您在Search_Tool表單中創建一個事件,該事件在用戶找到所需的內容時引發。
主表單已訂閱該表單,然后可以訪問其數據網格。
Class Search_Tool
Inherits Form
Public Event SearchResultFound(resultId As Integer)
Private Sub OnUserClickedARowOrPressedAButtonOrWhatever()
RaiseEvent SearchResultFound(currentRow.Id)
End Sub
End Class
並以主要形式存在。
Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
Dim ST = New Search_Tool(DGV_Ongoing.DataSource, AgencyOptions, EthnicityOptions, EmploymentTypeOptions, EmploymentStatusOptions,
CategoryOptions, OutcomeOptions)
AddHandler ST.SearchResultFound, Sub(result As Integer)
'handle your result id here.
MessageBox.Show("User selected row " & result)
End Sub
ST.Show()
End Sub
這樣做的好處是SearchForm不需要知道它在使用什么。 它只是在這里給出結果,使以后更容易重用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.