2013-12-08 42 views
2

好吧,我有2類誰可以一個標籤頁添加到TabControl無法從與事件的用戶控件添加標籤頁

  • 通訊:類,使用事件
  • GeneralChat:用戶控件,雙擊在列表框中

我這樣做,這樣下去

如下功能的方式是從GeneralChat

句柄時雙擊創建一個新的標籤頁

Private Sub ListConnecte_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles ListConnecte.MouseDoubleClick 
    addTab(ListConnecte.SelectedItems(0)) 'I've simplified the function, but it's not passing an empty value 
End Sub 

創建標籤頁

Private Sub addTab(sUser As String) 
    Dim tp As New TabPage(sUser) 
    Dim pc As New PrivateChat(Me, weComs, sUser) 

    pc.Dock = DockStyle.Fill 
    tp.Controls.Add(pc) 

    If (Me.InvokeRequired) Then 
     Me.Invoke(New AddTabDelegate(AddressOf MainForm.addTab), tp) 
    Else 
     MainForm.addTab(tp) 
    End If 

    listChat.Add(sUser, tp) 
End Sub 

它然後再在可變TabControlChat已創建MainForm,然後將其添加到TabControlChat.TabPages

所以我的問題是,當我雙擊,一切工作正常,但是當我這樣稱呼它

Private Sub ReceivedString_weComs(Sender As Object, e As WithParamReceivedString) Handles weComs.ReceivedString 
    Select Case e.Identifier 
     Case Communication.enumTags.PrivateChat 
      Dim sNom() As String = e.ReceivedString.Split(New String() {"#*#"}, StringSplitOptions.RemoveEmptyEntries) 

      If (Not listChat.ContainsKey(sNom(0))) Then 
       addTab(sNom(0)) 
      Else 
       RaiseEvent PrivateChatString(Me, New ReceivedStringEventArgs(e.ReceivedString)) 
      End If 
    End Select 
End Sub 

所以這個功能去的先例通話做了同樣的路徑,但一個添加的TabControl和通信類它不

我真的試過了很多,問我更多的問題有關調試,如果我不是很清楚,希望你能幫到:S

+2

您在Invoke調用中使用MainForm是個問題。 MainForm是一種類型,它不是一個對象。在VB.NET中很方便,但在工作線程上運行的代碼中絕對是致命的。現在「MainForm」成爲* another * MainForm對象的一個​​實例。一個由工作線程擁有,不可見,因爲它的Show()方法從未被調用過。您*必須*使用用戶正在查看的MainForm對象的適當引用。像我這樣的。 –

回答

0

在第二段代碼中,你實際上是指MainForm的一個對象,(它實際上是不可見的,直到你調用.show方法)。您必須使用原始的MainForm,它已經對用戶可見。