2013-07-03 23 views
2

這是我起牀的代碼,它沒有錯誤,但它似乎沒有工作。可以以某種方式告訴我它有什麼問題嗎?如何在不同的線程中使用Application.OpenForms的代理

 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 
+0

什麼不起作用?是否有特定的錯誤信息? –

+0

'.Focused()'從來不會在我關注的任何表單上觸發。我認爲也許是因爲我是突破點,而且在斷點被擊中的時候沒有集中注意力。但是我沒有把斷點關掉並且完全關注表單並且仍然不會彈出'failedLoginForm'。也許你知道更容易做到這一點的原因,我希望failedLoginForm在當前關注的任何表單的中心彈出。 – SSpoke

回答

1

,而不是使用重點,我不認爲作品非常好形式,可以嘗試使用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 
+0

這是完美的不知道'ActiveForm'。委託的東西不再需要ActiveForm,一切都很完美,但'failedLoginForm.Parent = frmCurrentForm'使得對話框不再出現,你知道那裏有什麼嗎? – SSpoke

+0

我也試過'failedLoginForm.ShowDialog(Form.ActiveForm)',它仍然不彈出。但是,當然''失敗LoginForm.ShowDialog()'效果很好,只是不會彈出它背後的窗體的中心。 – SSpoke

+0

ShowDialog將正常工作,但不要使用Form.ActiveForm作爲父項,因爲它可能會根據時間設置爲failedLoginForm。相反,在創建failedLoginForm的新實例之前,請將ActiveForm保存在局部變量中,然後將該局部變量傳遞給ShowDialog。 –

相關問題