我正在尝试根据从DataGridView中选择的行打开同一表单的多个实例。 我当前的代码有效,但在进入下一个表单之前等待每个表单关闭。 我需要同时打开表单,但仍然可以通过事件向主表单报告。
Public Class frmARLog
Private WithEvents edit As frmEditARLog
'WithEvents: indicate that we want to receive events raised by this object
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
fill()
End Sub
Sub fill()
Dim AR As AltARLog = New AltARLog
dgvARLog.DataSource = AR.getTable
End Sub
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
Dim iRowIndex As Integer
For i As Integer = 0 To dgvARLog.SelectedCells.Count - 1
iRowIndex = dgvARLog.SelectedCells.Item(i).RowIndex
openEditForm(dgvARLog.Rows(iRowIndex).Cells(0).Value.ToString, iRowIndex)
Next
End Sub
Sub openEditForm(arid As Integer, row As Integer)
edit = New frmEditARLog(arid)
AddHandler edit.FormClosing, AddressOf ItIsClosing
AddHandler edit.FormClosed, AddressOf ItIsClosed
edit.ShowDialog(Me)
End Sub
Private Sub ItIsClosing(sender As Object, e As FormClosingEventArgs)
If edit.Successful Then
fill()
End If
RemoveHandler edit.FormClosing, AddressOf ItIsClosing
End Sub
Private Sub ItIsClosed(sender As Object, e As FormClosedEventArgs)
RemoveHandler edit.FormClosed, AddressOf ItIsClosed
edit = Nothing
End Sub
End Class
答案 0 :(得分:0)
使用edit.ShowDialog(Me)
,你告诉它像对话框一样锁定调用表单。
您想将此行更改为:edit.Show(Me)
另外请注意,您将只允许在此代码中引用一个孩子。您可能需要考虑是否需要存储对所有表单的引用。
我建议不要存储它们。注册您的活动,然后显示。事件仍会按预期触发,然后您需要使用事件sender
的{{1}}参数,这将是调用子表单。
将其插入每个事件处理程序的第一行。
Sub