显示 MDI 子窗体后等待 - DoEvents 的替代方法
Wait after showing MDI child form - alternative to DoEvents
一位同事对我的应用程序进行了一些改进,但与此同时,他将整个应用程序更改为使用 MDI 子窗体。因此,我以前可以使用 ShowDialog() 使子例程等到新表单关闭后再继续,现在我不能了。我改编了一些旧的 VB6 代码来尝试复制相同的功能,但它使用 DoEvents() 并且我被告知要不惜一切代价避免这种情况。
在这一点上,我不禁觉得我的方法有一个根本性的缺陷,可能有更简单的方法来完成同样的任务。
Private Sub Label_EditNode_Click(sender As Object, e As EventArgs) Handles Label_EditNode.Click
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.myResultText = txbNodeClick.Text
}
Frm.Show()
Call WaitOnFormUnload()
txbNodeClick.Text = Frm.myResultText
End Sub
Public Sub WaitOnFormUnload()
Dim bIsLoaded As Boolean = True
Dim FormName As String = Me.MdiParent.ActiveMdiChild.Name ' Form.ActiveForm.Name
Do While bIsLoaded
bIsLoaded = False
For Each f As Form In Application.OpenForms
If f.Name = FormName Then
bIsLoaded = True
Application.DoEvents()
Exit For ' breaks the for loop
End If
Next f
Loop
End Sub
像这样:
Private Sub Label_EditNode_Click(sender As Object, e As EventArgs) Handles Label_EditNode.Click
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.myResultText = txbNodeClick.Text
}
AddHandler Frm.FormClosing, AddressOf RetrieveBlahData
Frm.Show()
End Sub
Private Sub RetrieveBlahData(sender as Object, e as FormClosingEventArgs)
txbNodeClick.Text = DirectCast(sender, frmVariableEditor).myResultText
End Sub
可能没有比这更小的了,除了可能共享一个数据对象:
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.DataSetToBindTo = Me.DataSetWhatever
}
您必须重新编码您的表单,以便它接受传入的数据集(或其他数据容器)并从中获取数据(数据绑定)..然后当编辑器表单更改数据时,它实际上也在主窗体中更改原始数据集,因为它们是同一回事
一位同事对我的应用程序进行了一些改进,但与此同时,他将整个应用程序更改为使用 MDI 子窗体。因此,我以前可以使用 ShowDialog() 使子例程等到新表单关闭后再继续,现在我不能了。我改编了一些旧的 VB6 代码来尝试复制相同的功能,但它使用 DoEvents() 并且我被告知要不惜一切代价避免这种情况。
在这一点上,我不禁觉得我的方法有一个根本性的缺陷,可能有更简单的方法来完成同样的任务。
Private Sub Label_EditNode_Click(sender As Object, e As EventArgs) Handles Label_EditNode.Click
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.myResultText = txbNodeClick.Text
}
Frm.Show()
Call WaitOnFormUnload()
txbNodeClick.Text = Frm.myResultText
End Sub
Public Sub WaitOnFormUnload()
Dim bIsLoaded As Boolean = True
Dim FormName As String = Me.MdiParent.ActiveMdiChild.Name ' Form.ActiveForm.Name
Do While bIsLoaded
bIsLoaded = False
For Each f As Form In Application.OpenForms
If f.Name = FormName Then
bIsLoaded = True
Application.DoEvents()
Exit For ' breaks the for loop
End If
Next f
Loop
End Sub
像这样:
Private Sub Label_EditNode_Click(sender As Object, e As EventArgs) Handles Label_EditNode.Click
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.myResultText = txbNodeClick.Text
}
AddHandler Frm.FormClosing, AddressOf RetrieveBlahData
Frm.Show()
End Sub
Private Sub RetrieveBlahData(sender as Object, e as FormClosingEventArgs)
txbNodeClick.Text = DirectCast(sender, frmVariableEditor).myResultText
End Sub
可能没有比这更小的了,除了可能共享一个数据对象:
Dim Frm As New frmVariableEditor With {
.Tag = "FrmVariableEditor",
.MdiParent = FrmMDIMain,
.Dock = DockStyle.Fill,
.Location = Me.Location,
.Size = Me.Size,
.myFieldName = "NodeClick",
.DataSetToBindTo = Me.DataSetWhatever
}
您必须重新编码您的表单,以便它接受传入的数据集(或其他数据容器)并从中获取数据(数据绑定)..然后当编辑器表单更改数据时,它实际上也在主窗体中更改原始数据集,因为它们是同一回事