为什么事件不会改变图像?

时间:2012-01-10 05:53:03

标签: vb.net event-handling mdi

我一直在努力解决这个问题而且无法理解这些事件。有人可以帮我理解我的代码中的事件过程吗?或者告诉我为什么我的图像在运行代码时不会切换?

类和成员的声明

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)

在我的Connect Sub中

'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容器的Shell实例。

所有图片都在资源中并正确拼写和引用。 MessageBox将弹出,但图像不会改变,除了Join。

我不是在尝试Code Dump,只是想确保你能看到我在看什么,并允许你提供更好的建议。

感谢所有帮助!

修改

好吧,我发现这很奇怪......我觉得我越来越近了。当消息框未被注释掉时,图像将会改变,当它被注释掉时,图像不会改变。 关于如何使其发挥作用的任何更好的建议?

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

2 个答案:

答案 0 :(得分:0)

添加了Application.DoEvents()并允许它工作。不确定为什么,但如果有人能解释那将是真棒!谢谢!

答案 1 :(得分:0)

尝试在更改图片后致电instanceShellProp.imgP2P.Refresh()

Microsoft Refresh的文档说:

“强制控件使其客户区无效,并立即重绘自身和任何子控件。”