如何在不同的线程中使用Application.OpenForms的委托

时间:2013-07-03 00:55:04

标签: vb.net action parent showdialog anonymous-delegates

这是我开始使用的代码,它没有错误,但它似乎不起作用。能以某种方式告诉我它有什么问题吗?

        Dim frmCurrentForm As Form
        Dim wasFocused As Boolean = False

        For Each frmCurrentForm In Application.OpenForms
            If Not frmCurrentForm Is Nothing Then
                Dim action As Action(Of Form)
                action = Sub(form)
                             If form.Focused() Then
                                 Dim failedLoginForm As New frmFailedLogin
                                 failedLoginForm.setError("failed blah blah")
                                 'failedLoginForm.Parent = form
                                 failedLoginForm.StartPosition = FormStartPosition.CenterParent
                                 failedLoginForm.ShowDialog()
                                 wasFocused = True
                             End If
                         End Sub

                If (frmCurrentForm.InvokeRequired) Then
                    frmCurrentForm.Invoke(action, New Object() {frmCurrentForm})
                Else
                    action(frmCurrentForm)
                End If

                If wasFocused Then
                    Exit For
                End If
            End If
        Next

1 个答案:

答案 0 :(得分:1)

而不是使用我认为不适合表单的Focused,请尝试使用Form.ActiveForm

    Dim frmCurrentForm As Form

    frmCurrentForm = Form.ActiveForm
    If Not frmCurrentForm Is Nothing Then
        Dim action As Action(Of Form)
        action = Sub(form)
                     Dim failedLoginForm As New Form2
                     failedLoginForm.setError("failed blah blah")
                     failedLoginForm.StartPosition = FormStartPosition.CenterParent
                     failedLoginForm.ShowDialog(form)
                 End Sub

        If (frmCurrentForm.InvokeRequired) Then
            frmCurrentForm.Invoke(action, New Object() {frmCurrentForm})
        Else
            action(frmCurrentForm)
        End If
    End If
相关问题