2012-01-10 46 views
0

我一直在努力解決這個問題,只是無法理解事件。有人能幫我理解我的代碼中的事件過程嗎?或者告訴我爲什麼我的圖像在運行代碼時不會切換?類爲什麼不會事件更改圖像?

聲明和成員

Partial Public Class Name 
    Implements IChat 

Private member As String 
Private instanceContext As InstanceContext 
Private participant As IChatChannel 
Private ostat As IOnlineStatus 
Private factory As DuplexChannelFactory(Of IChatChannel) 

在我的連接子

'Construct InstanceContext to handle messages on callback interface. 
' An instance of ChatApp is created and passed to the InstanceContext. 
instanceContext = New InstanceContext(Me) 

' Create the participant with the given endpoint configuration 
' Each participant opens a duplex channel to the mesh 
' participant is an instance of the chat application that has opened a channel to the mesh 
factory = New DuplexChannelFactory(Of IChatChannel)(instanceContext, "ChatEndpoint") 
participant = factory.CreateChannel() 

' Retrieve the PeerNode associated with the participant and register for online/offline events 
' PeerNode represents a node in the mesh. Mesh is the named collection of connected nodes. 
ostat = participant.GetProperty(Of IOnlineStatus)() 
AddHandler ostat.Online, AddressOf Me.OnOnline 
AddHandler ostat.Offline, AddressOf Me.OnOffline 

子例程應該改變形象

Public Sub Join(ByVal member As String) Implements IChat.Join 
    instanceShellProp.imgP2P.Image = Namespace.My.Resources.Offline 
    MsgBox("JOINED OFFLINE") 
End Sub 

Public Sub Leave1(ByVal member As String) Implements IChat.Leave 
    instanceShellProp.imgP2P.Image = Namespace.My.Resources.Disconnected 
    MsgBox("NOT CONNECTED") 
End Sub 

Public Sub OnOnline(ByVal sender As Object, ByVal e As EventArgs) 
    instanceShellProp.imgP2P.Image = Namespace.My.Resources.Online 
    MsgBox("JOINED ONLINE") 
End Sub 

Public Sub OnOffline(ByVal sender As Object, ByVal e As EventArgs) 
    instanceShellProp.imgP2P.Image = Namespace.My.Resources.Offline 
    MsgBox("JOINED OFFLINE") 
End Sub 

instanceShellProp返回殼牌的實例是一個MDI容器。

所有圖像都在資源中並正確拼寫和引用。消息框將彈出,但圖像不會改變,除了加入。

我不是在嘗試代碼轉儲,只是試圖確保您可以看到我正在查看的內容並允許您提供更好的建議。

所有幫助表示讚賞!

編輯

好吧,我發現這個奇怪的......我覺得我越來越接近。當消息框未被註釋掉時,圖像將會改變,當註釋掉時圖像不會改變。 有關如何使這項工作有更好的建議嗎?

Public Sub OnOnline(ByVal sender As Object, ByVal e As EventArgs) 
    With instanceShellProp.imgP2P 
     .Image = Nothing 
     .Visible = True 
    End With 
    'MsgBox("JOINED ONLINE") 
    With instanceShellProp.imgP2P 
     .Image = Namespace.My.Resources.Online 
     .Visible = True 
    End With 
End Sub 

回答

0

增加了Application.DoEvents(),它允許它工作。不知道爲什麼,但如果有人能解釋這將是真棒!謝謝!

0

嘗試在更改圖像後調用instanceShellProp.imgP2P.Refresh()

微軟爲Refresh文檔說:

「強制控制無效的客戶區,並立即重繪自己和任何子控件。」

+0

我不確定這是否真的改變了它所做的一切。但是,如果你能指出我在某個線程中處理某些事件的方法的總體方向,那將是非常棒的。 (我是新的事件,從來沒有接觸過線程)。看起來事件並不總是在另一臺計算機連接時運行(強制狀態爲聯機) – 2012-01-10 22:07:15

相關問題