Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 从多个动态picturebox更改图像_.net_Vb.net_User Controls - Fatal编程技术网

.net 从多个动态picturebox更改图像

.net 从多个动态picturebox更改图像,.net,vb.net,user-controls,.net,Vb.net,User Controls,我正在用VB.NET制作一个带有状态图像的联系人列表。 我正在从MSSQL加载此列表,但当我重新加载列表时,它会闪烁 此列表是一个TableLayoutPanel,其中包含动态创建的图片框和标签 我的问题是: 当你重新加载我的联系人而不是重新加载整个列表时,我如何在动态picturebox中更改我的图像 创建表的我的代码: While UserData.Read If UserData("Status").ToString = "Online" Then If Use

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

此列表是一个TableLayoutPanel,其中包含动态创建的图片框和标签

我的问题是:

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

创建表的我的代码:

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
我的联系人列表的图像


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

解决方案资源管理器窗口中,右键单击,选择添加,然后选择用户控制。我添加了一个TableLayoutPanel、两个PictureBox和一个标签(当然都有专有名称)。还有一个
图像列表
,可以快速抓取您的图像:

仅此一项就可以替换许多代码行,您必须创建新控件,然后反复设置相同的属性。要使它们充当逻辑对象,您可以添加属性来抽象选择图像的细节等:

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
Id可以唯一地标识每个聊天参与者。名称通常不够(所以已经超过了),您需要一种识别方法来将控件链接到用户。(另一种选择是用户列表中的
ChatterBox
引用,它是对相关
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
每次搜索都是次优的,一个
Id
比一个简单的名字要好。最理想的是一个
ChatUser
类,以及应用程序必须按用户存储的所有其他内容。该类应包括对控件的引用,以便在状态更改或其他任何情况下,该类可以简单地:

myChatterBox.Status = myStatus
结果:

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

作为一个额外的好处,因为您不再创建单个控件,而且由于
UserControl
继承自
组件
,如果/当这些控件被删除时,您不必担心泄漏

A必须阅读:

很有趣。我记得你刚发布时建议使用用户控件。如果你有一个用户控件,那么你可以更轻松地处理这类事情……然后你可以编写方法来处理其中的任何一个……这意味着你应该创建一个用户控件,该控件可以容纳关于此人的所有信息……这也是你上面的代码很长且复杂的原因自找麻烦。以上所有内容都可以减半甚至更多。如果你忘记了,我会发表我的评论。我同意@Codexer,我不喜欢用UserControls代替自定义控件。你的表单只是一堆显示相关信息的无关控件。在我的工具中找不到usercontroll??我对这个xd非常陌生,你有什么例子吗?您必须将其添加到项目中。右键单击解决方案,然后添加新的用户控件。很好:)它工作得很好,您是对的。需要更少的代码@普卢蒂克斯我仍在努力解决一些问题-AddHandler当我点击AddHandler C时。点击,AddressOf Chatbox我就不能点击它了-更新创建这些控件时,如何执行这些控件的更新?这些控件是动态生成的。当然,我不知道您实现的细节。我使用了TLP,所以UC中没有供用户单击的区域。通过将子控件事件映射到UC处理程序
Private子Chatter\u Click(发送者作为对象,e作为事件参数)来处理我。Click,blChName。Click,pbMStatus。Click,pbUStatus。Click
。添加它们时,AddHandler将指定聊天盒(UC)单击事件。正如答案所解释/显示的,通过设置它们中任何一个的相关属性进行更新。我应该提到,我真正做到这一点的方法是为用户使用一个固定的图像和一个气球,气球是一个透明的PNG剪贴画,以显示后面的颜色。与其重新分配图像,我只需更改图像背后的背景颜色。如果它是一个标签,我可以向它添加文本来表示未知的“?”和未读消息的数字。使用这种方法,您会很快发现MsgStatus是不需要的-状态可以从未读MsgStatus的数量中派生出来,就像您所做的那样,它是完美的,但是onclick是一个问题。我尝试了不同的方法,比如c.click,c.lblChName.name,但是没有运气
myChatterBox.Status = myStatus