从多个动态图片框中更改图像

Change the image from multiple dynamic picturebox(从多个动态图片框中更改图像)
本文介绍了从多个动态图片框中更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有状态图像的 VB.NET 制作联系人列表.我正在从 MSSQL 加载此列表,但是当我重新加载列表时它会闪烁.

此列表是一个 TableLayoutPanel,带有动态创建的图片框和标签.

我的问题是:

当您重新加载我的联系人而不是重新加载整个列表时,我如何在动态图片框中更改我的图像.

我创建表格的代码:

当 UserData.ReadIf UserData("Status").ToString = "Online" ThenIf UserData("NieuwBericht").ToString = "Ja" Then将新图片框变暗为新图片框newPictureBox.Image = My.Resources.greenchatnewPictureBox.Visible = True新图片框宽度 = 30newPictureBox.Height = 30newPictureBox.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox)将 newPictureBox2 调暗为新的 PictureBoxnewPictureBox2.Image = My.Resources.greenbubblechatnewPictureBox2.Visible = TruenewPictureBox2.Width = 30newPictureBox2.Height = 30newPictureBox2.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox2.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox2)将 newLabel 调暗为新标签AddHandler newLabel.Click, AddressOf ChatboxnewLabel.Text = UserData("Voornaam").ToString &" " &UserData("Achternaam").ToStringnewLabel.Name = UserData("用户名").ToStringnewLabel.Font = New Font("Microsoft sans serif", 12)newLabel.Dock = DockStyle.FillnewLabel.TextAlign = ContentAlignment.MiddleLeftnewLabel.Visible = TrueChatContactList.Controls.Add(newLabel)ElseIf UserData("NieuwBericht").ToString = "Nee" Then将新图片框变暗为新图片框newPictureBox.Image = My.Resources.greenchatnewPictureBox.Visible = True新图片框宽度 = 30newPictureBox.Height = 30newPictureBox.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox)将 newPictureBox2 调暗为新的 PictureBoxnewPictureBox2.Image = My.Resources.greybubblechatnewPictureBox2.Visible = TruenewPictureBox2.Width = 30newPictureBox2.Height = 30newPictureBox2.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox2.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox2)将 newLabel 调暗为新标签AddHandler newLabel.Click, AddressOf ChatboxnewLabel.Text = UserData("Voornaam").ToString &" " &UserData("Achternaam").ToStringnewLabel.Name = UserData("用户名").ToStringnewLabel.Font = New Font("Microsoft sans serif", 12)newLabel.Dock = DockStyle.FillnewLabel.TextAlign = ContentAlignment.MiddleLeftnewLabel.Visible = TrueChatContactList.Controls.Add(newLabel)万一ElseIf UserData("Status").ToString = "Afwezig" ThenIf UserData("NieuwBericht").ToString = "Ja" Then将新图片框变暗为新图片框newPictureBox.Image = My.Resources.orangechatnewPictureBox.Visible = True新图片框宽度 = 30newPictureBox.Height = 30newPictureBox.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox)将 newPictureBox2 调暗为新的 PictureBoxnewPictureBox2.Image = My.Resources.greenbubblechatnewPictureBox2.Visible = TruenewPictureBox2.Width = 30newPictureBox2.Height = 30newPictureBox2.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox2.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox2)将 newLabel 调暗为新标签AddHandler newLabel.Click, AddressOf ChatboxnewLabel.Text = UserData("Voornaam").ToString &" " &UserData("Achternaam").ToStringnewLabel.Name = UserData("用户名").ToStringnewLabel.Font = New Font("Microsoft sans serif", 12)newLabel.Dock = DockStyle.FillnewLabel.TextAlign = ContentAlignment.MiddleLeftnewLabel.Visible = TrueChatContactList.Controls.Add(newLabel)ElseIf UserData("NieuwBericht").ToString = "Nee" Then将新图片框变暗为新图片框newPictureBox.Image = My.Resources.orangechatnewPictureBox.Visible = True新图片框宽度 = 30newPictureBox.Height = 30newPictureBox.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox)将 newPictureBox2 调暗为新的 PictureBoxnewPictureBox2.Image = My.Resources.greybubblechatnewPictureBox2.Visible = TruenewPictureBox2.Width = 30newPictureBox2.Height = 30newPictureBox2.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox2.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox2)将 newLabel 调暗为新标签AddHandler newLabel.Click, AddressOf ChatboxnewLabel.Text = UserData("Voornaam").ToString &" " &UserData("Achternaam").ToStringnewLabel.Name = UserData("用户名").ToStringnewLabel.Font = New Font("Microsoft sans serif", 12)newLabel.Dock = DockStyle.FillnewLabel.TextAlign = ContentAlignment.MiddleLeftnewLabel.Visible = TrueChatContactList.Controls.Add(newLabel)万一ElseIf UserData("Status").ToString = "Offline" ThenIf UserData("NieuwBericht").ToString = "Ja" Then将新图片框变暗为新图片框newPictureBox.Image = My.Resources.redchatnewPictureBox.Visible = True新图片框宽度 = 30newPictureBox.Height = 30newPictureBox.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox)将 newPictureBox2 调暗为新的 PictureBoxnewPictureBox2.Image = My.Resources.greenbubblechatnewPictureBox2.Visible = TruenewPictureBox2.Width = 30newPictureBox2.Height = 30newPictureBox2.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox2.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox2)将 newLabel 调暗为新标签AddHandler newLabel.Click, AddressOf ChatboxnewLabel.Text = UserData("Voornaam").ToString &" " &UserData("Achternaam").ToStringnewLabel.Name = UserData("用户名").ToStringnewLabel.Font = New Font("Microsoft sans serif", 12)newLabel.Dock = DockStyle.FillnewLabel.TextAlign = ContentAlignment.MiddleLeftnewLabel.Visible = TrueChatContactList.Controls.Add(newLabel)ElseIf UserData("NieuwBericht").ToString = "Nee" Then将新图片框变暗为新图片框newPictureBox.Image = My.Resources.redchatnewPictureBox.Visible = True新图片框宽度 = 30newPictureBox.Height = 30newPictureBox.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox)将 newPictureBox2 调暗为新的 PictureBoxnewPictureBox2.Image = My.Resources.greybubblechatnewPictureBox2.Visible = TruenewPictureBox2.Width = 30newPictureBox2.Height = 30newPictureBox2.SizeMode = PictureBoxSizeMode.ZoomnewPictureBox2.Name = UserData("用户名").ToStringChatContactList.Controls.Add(newPictureBox2)将 newLabel 调暗为新标签AddHandler newLabel.Click, AddressOf ChatboxnewLabel.Text = UserData("Voornaam").ToString &" " &UserData("Achternaam").ToStringnewLabel.Name = UserData("用户名").ToStringnewLabel.Font = New Font("Microsoft sans serif", 12)newLabel.Dock = DockStyle.FillnewLabel.TextAlign = ContentAlignment.MiddleLeftnewLabel.Visible = TrueChatContactList.Controls.Add(newLabel)万一万一结束时

我的联系人列表的图片

解决方案

UserControl 有点像子窗体.它是各种控件的容器,这些控件一起起作用或代表某个逻辑单元.在这种情况下,它将是一些用户状态(属性)的可视化表示.无需创建和管理单独的控件,您可以将它们放在一个 UC 上并封装大部分代码来管理它们.

Solution Explorer 窗口中,右键单击,选择 Add,然后选择 UserControl.我添加了一个 TableLayoutPanel、2 个图片框和一个标签(当然,它们都有正确的名称).还有一个 ImageList 可以快速抓取您的图像:

这个单独替换了许多代码行,您必须创建新控件然后一遍又一遍地设置相同的属性.为了使它们充当逻辑对象,您可以添加抽象选择图像等细节的属性:

公开课闲聊公共枚举聊天状态未知在线的离开离线结束枚举公共枚举 ChatMsgStatusundefined ' kludge 强制初始状态未知[新的]读结束枚举' 所有聊天实例的一组图像私人共享图像作为图像()公共属性 ChatId 作为 Int32 ' 还是 guid?Private chName As String = ""公共只读属性 ChatName 作为字符串得到返回 chName结束获取结束属性Private mStatus As ChatMsgStatus = ChatMsgStatus.Undefined公共属性 MsgStatus 作为 ChatMsgStatus得到返回 mStatus结束获取设置(值作为 ChatMsgStatus)If (value <> mStatus) Then选择案例值案例 ChatMsgStatus.NewpbMStatus.Image = Imgs(3)案例 ChatMsgStatus.ReadpbMStatus.Image = Imgs(4)其他情况pbMStatus.Image = Imgs(4)结束选择万一mStatus = 值结束集结束属性私人 chStatus 作为 ChatStatus = ChatStatus.Unknown公共财产状态为 ChatStatus得到返回 chStatus结束获取设置(值作为 ChatStatus)如果值 <>chStatus 然后选择案例值案例 ChatStatus.OnlinepbUStatus.Image = Imgs(0)案例 ChatStatus.AwaypbUStatus.Image = Imgs(1)案例 ChatStatus.OfflinepbUStatus.Image = Imgs(2)其他情况结束选择万一chStatus = 值结束集结束属性公共子新()' 设计者需要此调用.初始化组件()如果 Imgs 什么都不是,那么Imgs = 新图像() {My.Resources.ChatUserGrn,My.Resources.ChatUserYlw,My.Resources.ChatUserRed,My.Resources.ChatBalloonGrn,My.Resources.ChatBalloonGry}' 看注释万一万一' 在 InitializeComponent() 调用之后添加任何初始化.结束子' 不需要创建一个没有标识符的Public Sub New(n As Int32, cname As String)MyClass.New()' 默认初始值:chName = cname聊天 ID = nlblChName.Text = cnameMe.Status = ChatStatus.OnlineMe.MsgStatus = ChatMsgStatus.Unknown结束子结束类

(编辑)我不喜欢使用 ImageList 的结果.此版本从资源中加载图像数组.除了只加载一份供所有用户使用的 GreenUser 副本之外,它还允许您根据需要对其进行定制.例如,将背景颜色更改为 SystemColors.Window 以匹配用户的主题.如果您还使用标签而不是图片框,则可以将 Text 属性用于?"甚至指出新消息的数量.

我确信它还有更多内容,我可以想到几件事 想让它知道(例如用未读的数量覆盖绿色气球消息).但这里的重点是封装的概念

当然,它们可以用相当少的代码来创建.在此过程中,您可以通过设置相关属性来更改任一图像的状态.

作为一个额外的好处,因为您不再创建单独的控件,并且因为 UserControlComponent 继承,所以如果/当这些被删除时,您不必担心泄漏.

必读:
创建 Windows 窗体用户控件

I'm making a contactlist with VB.NET with status images. I'm loading this list from MSSQL but when i reload the list it flickers.

This list is a TableLayoutPanel with dynamic created pictureboxes and labels.

My question is:

How can i change my image inside a dynamic picturebox when u reload my contacts instead of reloading the entire list.

My code to create the table:

While UserData.Read

    If UserData("Status").ToString = "Online" Then

        If UserData("NieuwBericht").ToString = "Ja" Then

            Dim newPictureBox As New PictureBox
            newPictureBox.Image = My.Resources.greenchat
            newPictureBox.Visible = True
            newPictureBox.Width = 30
            newPictureBox.Height = 30
            newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox)

            Dim newPictureBox2 As New PictureBox
            newPictureBox2.Image = My.Resources.greenbubblechat
            newPictureBox2.Visible = True
            newPictureBox2.Width = 30
            newPictureBox2.Height = 30
            newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox2.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox2)

            Dim newLabel As New Label
            AddHandler newLabel.Click, AddressOf Chatbox
            newLabel.Text = UserData("Voornaam").ToString & " " & UserData("Achternaam").ToString
            newLabel.Name = UserData("Username").ToString
            newLabel.Font = New Font("Microsoft sans serif", 12)
            newLabel.Dock = DockStyle.Fill
            newLabel.TextAlign = ContentAlignment.MiddleLeft
            newLabel.Visible = True
            ChatContactList.Controls.Add(newLabel)

        ElseIf UserData("NieuwBericht").ToString = "Nee" Then

            Dim newPictureBox As New PictureBox
            newPictureBox.Image = My.Resources.greenchat
            newPictureBox.Visible = True
            newPictureBox.Width = 30
            newPictureBox.Height = 30
            newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox)

            Dim newPictureBox2 As New PictureBox
            newPictureBox2.Image = My.Resources.greybubblechat
            newPictureBox2.Visible = True
            newPictureBox2.Width = 30
            newPictureBox2.Height = 30
            newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox2.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox2)

            Dim newLabel As New Label
            AddHandler newLabel.Click, AddressOf Chatbox
            newLabel.Text = UserData("Voornaam").ToString & " " & UserData("Achternaam").ToString
            newLabel.Name = UserData("Username").ToString
            newLabel.Font = New Font("Microsoft sans serif", 12)
            newLabel.Dock = DockStyle.Fill
            newLabel.TextAlign = ContentAlignment.MiddleLeft
            newLabel.Visible = True
            ChatContactList.Controls.Add(newLabel)

        End If

    ElseIf UserData("Status").ToString = "Afwezig" Then

        If UserData("NieuwBericht").ToString = "Ja" Then

            Dim newPictureBox As New PictureBox
            newPictureBox.Image = My.Resources.orangechat
            newPictureBox.Visible = True
            newPictureBox.Width = 30
            newPictureBox.Height = 30
            newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox)

            Dim newPictureBox2 As New PictureBox
            newPictureBox2.Image = My.Resources.greenbubblechat
            newPictureBox2.Visible = True
            newPictureBox2.Width = 30
            newPictureBox2.Height = 30
            newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox2.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox2)

            Dim newLabel As New Label
            AddHandler newLabel.Click, AddressOf Chatbox
            newLabel.Text = UserData("Voornaam").ToString & " " & UserData("Achternaam").ToString
            newLabel.Name = UserData("Username").ToString
            newLabel.Font = New Font("Microsoft sans serif", 12)
            newLabel.Dock = DockStyle.Fill
            newLabel.TextAlign = ContentAlignment.MiddleLeft
            newLabel.Visible = True
            ChatContactList.Controls.Add(newLabel)

        ElseIf UserData("NieuwBericht").ToString = "Nee" Then

            Dim newPictureBox As New PictureBox
            newPictureBox.Image = My.Resources.orangechat
            newPictureBox.Visible = True
            newPictureBox.Width = 30
            newPictureBox.Height = 30
            newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox)

            Dim newPictureBox2 As New PictureBox
            newPictureBox2.Image = My.Resources.greybubblechat
            newPictureBox2.Visible = True
            newPictureBox2.Width = 30
            newPictureBox2.Height = 30
            newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox2.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox2)

            Dim newLabel As New Label
            AddHandler newLabel.Click, AddressOf Chatbox
            newLabel.Text = UserData("Voornaam").ToString & " " & UserData("Achternaam").ToString
            newLabel.Name = UserData("Username").ToString
            newLabel.Font = New Font("Microsoft sans serif", 12)
            newLabel.Dock = DockStyle.Fill
            newLabel.TextAlign = ContentAlignment.MiddleLeft
            newLabel.Visible = True
            ChatContactList.Controls.Add(newLabel)

        End If

    ElseIf UserData("Status").ToString = "Offline" Then

        If UserData("NieuwBericht").ToString = "Ja" Then

            Dim newPictureBox As New PictureBox
            newPictureBox.Image = My.Resources.redchat
            newPictureBox.Visible = True
            newPictureBox.Width = 30
            newPictureBox.Height = 30
            newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox)

            Dim newPictureBox2 As New PictureBox
            newPictureBox2.Image = My.Resources.greenbubblechat
            newPictureBox2.Visible = True
            newPictureBox2.Width = 30
            newPictureBox2.Height = 30
            newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox2.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox2)

            Dim newLabel As New Label
            AddHandler newLabel.Click, AddressOf Chatbox
            newLabel.Text = UserData("Voornaam").ToString & " " & UserData("Achternaam").ToString
            newLabel.Name = UserData("Username").ToString
            newLabel.Font = New Font("Microsoft sans serif", 12)
            newLabel.Dock = DockStyle.Fill
            newLabel.TextAlign = ContentAlignment.MiddleLeft
            newLabel.Visible = True
            ChatContactList.Controls.Add(newLabel)

        ElseIf UserData("NieuwBericht").ToString = "Nee" Then

            Dim newPictureBox As New PictureBox
            newPictureBox.Image = My.Resources.redchat
            newPictureBox.Visible = True
            newPictureBox.Width = 30
            newPictureBox.Height = 30
            newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox)

            Dim newPictureBox2 As New PictureBox
            newPictureBox2.Image = My.Resources.greybubblechat
            newPictureBox2.Visible = True
            newPictureBox2.Width = 30
            newPictureBox2.Height = 30
            newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
            newPictureBox2.Name = UserData("Username").ToString
            ChatContactList.Controls.Add(newPictureBox2)

            Dim newLabel As New Label
            AddHandler newLabel.Click, AddressOf Chatbox
            newLabel.Text = UserData("Voornaam").ToString & " " & UserData("Achternaam").ToString
            newLabel.Name = UserData("Username").ToString
            newLabel.Font = New Font("Microsoft sans serif", 12)
            newLabel.Dock = DockStyle.Fill
            newLabel.TextAlign = ContentAlignment.MiddleLeft
            newLabel.Visible = True
            ChatContactList.Controls.Add(newLabel)

        End If

    End If

End While

Image of my contactlist

解决方案

A UserControl is a bit like a subform. It is a container for an assortment of controls which act together or represent some logical unit. In this case, it would be the visual representation of some user states (properties). Rather than creating and managing individual controls, you can put them on one UC and encapsulate much of the code to manage them.

In the Solution Explorer window, right click, select Add, then pick UserControl. I added a TableLayoutPanel, 2 PictureBoxes and a Label (all with proper names of course). There is also an ImageList with a quick grab of your images:

This alone replaces many many lines of code you have to create new controls and then setting the same properties over and over again. To make them act as a logical object, you can add properties which abstracts the minutiae of selecting images etc:

Public Class Chatter

    Public Enum ChatStatus
        Unknown
        Online
        Away
        Offline
    End Enum

    Public Enum ChatMsgStatus
        Undefined      ' kludge to force the initial state
        Unknown
        [New]
        Read
    End Enum

    ' one set of images for all chatter instances
    Private Shared Imgs As Image()

    Public Property ChatId As Int32         ' or guid?

    Private chName As String = ""
    Public ReadOnly Property ChatName As String
        Get
            Return chName
        End Get
    End Property

    Private mStatus As ChatMsgStatus = ChatMsgStatus.Undefined
    Public Property MsgStatus As ChatMsgStatus
        Get
            Return mStatus
        End Get
        Set(value As ChatMsgStatus)
            If (value <> mStatus) Then
                Select Case value
                    Case ChatMsgStatus.New
                        pbMStatus.Image = Imgs(3) 
                    Case ChatMsgStatus.Read
                        pbMStatus.Image = Imgs(4)
                    Case Else
                        pbMStatus.Image = Imgs(4)
                End Select
            End If
            mStatus = value
        End Set
    End Property

    Private chStatus As ChatStatus = ChatStatus.Unknown
    Public Property Status As ChatStatus
        Get
            Return chStatus
        End Get
        Set(value As ChatStatus)
            If value <> chStatus Then
                Select Case value
                    Case ChatStatus.Online
                        pbUStatus.Image = Imgs(0)
                    Case ChatStatus.Away
                        pbUStatus.Image = Imgs(1)
                    Case ChatStatus.Offline
                        pbUStatus.Image = Imgs(2)
                    Case Else
                End Select
            End If
            chStatus = value
        End Set
    End Property

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        If Imgs Is Nothing Then
            Imgs = New Image() {My.Resources.ChatUserGrn, My.Resources.ChatUserYlw, 
               My.Resources.ChatUserRed, My.Resources.ChatBalloonGrn,
               My.Resources.ChatBalloonGry}

               ' see note
        End If
    End If
        ' Add any initialization after the InitializeComponent() call.
    End Sub
    ' no need to create one without Identifiers
    Public Sub New(n As Int32, cname As String)
        MyClass.New()
        ' default intitial values:
        chName = cname
        ChatId = n

        lblChName.Text = cname
        Me.Status = ChatStatus.Online
        Me.MsgStatus = ChatMsgStatus.Unknown
    End Sub
End Class

(Edit) I didn't like the result using an ImageList. This version, loads an array of images from resources. Aside from only loading one copy of GreenUser for use by all users, it allows you to tailor it as needed. For instance change the backcolor to SystemColors.Window to match the user's theme. If you also use a label instead of a picturebox, you can use the Text property for "?" or even indicate the number of new messages.

I am sure there is more to it and I can think of several things I would want it to know (for instance overlay the green balloon with the number of unread messages). But the point here are the concepts of encapsulation, DRY and reusable code.

When you compile it, there will be a new Chatter control in the toolbox. Add some at runtime using the properties exposed:

Dim c As New Chatter(42, "Ziggy von Hausen")
flpChat.Controls.Add(c)

c = New Chatter(14, "ThDutoit")
c.MsgStatus = Chatter.ChatMsgStatus.New
flpChat.Controls.Add(c)

c = New Chatter(78, "Plutonix")
c.Status = Chatter.ChatStatus.Offline
flpChat.Controls.Add(c)

c = New Chatter(4, "Codexer")
c.MsgStatus = Chatter.ChatMsgStatus.New
c.Status = Chatter.ChatStatus.Away
flpChat.Controls.Add(c)

The Id would be something to uniquely identify each chat participant. The name is not usually enough (SO has over 50 pages of people named "Steve") and you will want a way to identify to link a control to a user. (An alternative, would be a ChatterBox reference in the user list which is a reference to the related UserControl:

Dim user = "Codexer"

Dim chatter = flpChat.Controls.
            OfType(Of Chatter).
            FirstOrDefault(Function(c) c.ChatName.StartsWith(user))
If chatter IsNot Nothing Then
    chatter.Status = chatter.ChatStatus.Online
End If

It is sub optimal to search each time and an Id would be better than a mere name. The ideal would be for a ChatUser class with all the other stuff the app has to store by user. The class should include a reference to the control so that when the status changes or whatever, the class could simply:

myChatterBox.Status = myStatus

Result:

Certainly they can be created with considerably less code. In the course of things, you can change the status of either image by just setting the related property.

As an added benefit, because you are no longer creating individual controls, and because UserControl inherits from Component you dont have to worry about leaks if/when these are removed.

A Must Read:
Creating a Windows Form User Control

这篇关于从多个动态图片框中更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)