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

前端之家收集整理的这篇文章主要介绍了.net – 从多个动态图片框中更改图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用带有状态图像的VB.NET制作联系人列表.
我正在从MSsql加载这个列表但是当我重新加载列表时它会闪烁.

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

我的问题是:

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

我创建表的代码

  1. While UserData.Read
  2.  
  3. If UserData("Status").ToString = "Online" Then
  4.  
  5. If UserData("NieuwBericht").ToString = "Ja" Then
  6.  
  7. Dim newPictureBox As New PictureBox
  8. newPictureBox.Image = My.Resources.greenchat
  9. newPictureBox.Visible = True
  10. newPictureBox.Width = 30
  11. newPictureBox.Height = 30
  12. newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
  13. newPictureBox.Name = UserData("Username").ToString
  14. ChatContactList.Controls.Add(newPictureBox)
  15.  
  16. Dim newPictureBox2 As New PictureBox
  17. newPictureBox2.Image = My.Resources.greenbubblechat
  18. newPictureBox2.Visible = True
  19. newPictureBox2.Width = 30
  20. newPictureBox2.Height = 30
  21. newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
  22. newPictureBox2.Name = UserData("Username").ToString
  23. ChatContactList.Controls.Add(newPictureBox2)
  24.  
  25. Dim newLabel As New Label
  26. AddHandler newLabel.Click,AddressOf ChatBox
  27. newLabel.Text = UserData("Voornaam").ToString & " " & UserData("Achternaam").ToString
  28. newLabel.Name = UserData("Username").ToString
  29. newLabel.Font = New Font("Microsoft sans serif",12)
  30. newLabel.Dock = DockStyle.Fill
  31. newLabel.TextAlign = ContentAlignment.MiddleLeft
  32. newLabel.Visible = True
  33. ChatContactList.Controls.Add(newLabel)
  34.  
  35. ElseIf UserData("NieuwBericht").ToString = "Nee" Then
  36.  
  37. Dim newPictureBox As New PictureBox
  38. newPictureBox.Image = My.Resources.greenchat
  39. newPictureBox.Visible = True
  40. newPictureBox.Width = 30
  41. newPictureBox.Height = 30
  42. newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
  43. newPictureBox.Name = UserData("Username").ToString
  44. ChatContactList.Controls.Add(newPictureBox)
  45.  
  46. Dim newPictureBox2 As New PictureBox
  47. newPictureBox2.Image = My.Resources.greybubblechat
  48. newPictureBox2.Visible = True
  49. newPictureBox2.Width = 30
  50. newPictureBox2.Height = 30
  51. newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
  52. newPictureBox2.Name = UserData("Username").ToString
  53. ChatContactList.Controls.Add(newPictureBox2)
  54.  
  55. Dim newLabel As New Label
  56. AddHandler newLabel.Click,12)
  57. newLabel.Dock = DockStyle.Fill
  58. newLabel.TextAlign = ContentAlignment.MiddleLeft
  59. newLabel.Visible = True
  60. ChatContactList.Controls.Add(newLabel)
  61.  
  62. End If
  63.  
  64. ElseIf UserData("Status").ToString = "Afwezig" Then
  65.  
  66. If UserData("NieuwBericht").ToString = "Ja" Then
  67.  
  68. Dim newPictureBox As New PictureBox
  69. newPictureBox.Image = My.Resources.orangechat
  70. newPictureBox.Visible = True
  71. newPictureBox.Width = 30
  72. newPictureBox.Height = 30
  73. newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
  74. newPictureBox.Name = UserData("Username").ToString
  75. ChatContactList.Controls.Add(newPictureBox)
  76.  
  77. Dim newPictureBox2 As New PictureBox
  78. newPictureBox2.Image = My.Resources.greenbubblechat
  79. newPictureBox2.Visible = True
  80. newPictureBox2.Width = 30
  81. newPictureBox2.Height = 30
  82. newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
  83. newPictureBox2.Name = UserData("Username").ToString
  84. ChatContactList.Controls.Add(newPictureBox2)
  85.  
  86. Dim newLabel As New Label
  87. AddHandler newLabel.Click,12)
  88. newLabel.Dock = DockStyle.Fill
  89. newLabel.TextAlign = ContentAlignment.MiddleLeft
  90. newLabel.Visible = True
  91. ChatContactList.Controls.Add(newLabel)
  92.  
  93. ElseIf UserData("NieuwBericht").ToString = "Nee" Then
  94.  
  95. Dim newPictureBox As New PictureBox
  96. newPictureBox.Image = My.Resources.orangechat
  97. newPictureBox.Visible = True
  98. newPictureBox.Width = 30
  99. newPictureBox.Height = 30
  100. newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
  101. newPictureBox.Name = UserData("Username").ToString
  102. ChatContactList.Controls.Add(newPictureBox)
  103.  
  104. Dim newPictureBox2 As New PictureBox
  105. newPictureBox2.Image = My.Resources.greybubblechat
  106. newPictureBox2.Visible = True
  107. newPictureBox2.Width = 30
  108. newPictureBox2.Height = 30
  109. newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
  110. newPictureBox2.Name = UserData("Username").ToString
  111. ChatContactList.Controls.Add(newPictureBox2)
  112.  
  113. Dim newLabel As New Label
  114. AddHandler newLabel.Click,12)
  115. newLabel.Dock = DockStyle.Fill
  116. newLabel.TextAlign = ContentAlignment.MiddleLeft
  117. newLabel.Visible = True
  118. ChatContactList.Controls.Add(newLabel)
  119.  
  120. End If
  121.  
  122. ElseIf UserData("Status").ToString = "Offline" Then
  123.  
  124. If UserData("NieuwBericht").ToString = "Ja" Then
  125.  
  126. Dim newPictureBox As New PictureBox
  127. newPictureBox.Image = My.Resources.redchat
  128. newPictureBox.Visible = True
  129. newPictureBox.Width = 30
  130. newPictureBox.Height = 30
  131. newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
  132. newPictureBox.Name = UserData("Username").ToString
  133. ChatContactList.Controls.Add(newPictureBox)
  134.  
  135. Dim newPictureBox2 As New PictureBox
  136. newPictureBox2.Image = My.Resources.greenbubblechat
  137. newPictureBox2.Visible = True
  138. newPictureBox2.Width = 30
  139. newPictureBox2.Height = 30
  140. newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
  141. newPictureBox2.Name = UserData("Username").ToString
  142. ChatContactList.Controls.Add(newPictureBox2)
  143.  
  144. Dim newLabel As New Label
  145. AddHandler newLabel.Click,12)
  146. newLabel.Dock = DockStyle.Fill
  147. newLabel.TextAlign = ContentAlignment.MiddleLeft
  148. newLabel.Visible = True
  149. ChatContactList.Controls.Add(newLabel)
  150.  
  151. ElseIf UserData("NieuwBericht").ToString = "Nee" Then
  152.  
  153. Dim newPictureBox As New PictureBox
  154. newPictureBox.Image = My.Resources.redchat
  155. newPictureBox.Visible = True
  156. newPictureBox.Width = 30
  157. newPictureBox.Height = 30
  158. newPictureBox.SizeMode = PictureBoxSizeMode.Zoom
  159. newPictureBox.Name = UserData("Username").ToString
  160. ChatContactList.Controls.Add(newPictureBox)
  161.  
  162. Dim newPictureBox2 As New PictureBox
  163. newPictureBox2.Image = My.Resources.greybubblechat
  164. newPictureBox2.Visible = True
  165. newPictureBox2.Width = 30
  166. newPictureBox2.Height = 30
  167. newPictureBox2.SizeMode = PictureBoxSizeMode.Zoom
  168. newPictureBox2.Name = UserData("Username").ToString
  169. ChatContactList.Controls.Add(newPictureBox2)
  170.  
  171. Dim newLabel As New Label
  172. AddHandler newLabel.Click,12)
  173. newLabel.Dock = DockStyle.Fill
  174. newLabel.TextAlign = ContentAlignment.MiddleLeft
  175. newLabel.Visible = True
  176. ChatContactList.Controls.Add(newLabel)
  177.  
  178. End If
  179.  
  180. End If
  181.  
  182. End While

我的联系人名单的图片

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

在Solution Explorer窗口中,右键单击,选择Add,然后选择UserControl.我添加了一个TableLayoutPanel,2个PictureBoxes和一个Label(当然所有都有正确的名称).还有一个ImageList,可快速抓取您的图像:

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

  1. Public Class Chatter
  2.  
  3. Public Enum ChatStatus
  4. Unknown
  5. Online
  6. Away
  7. Offline
  8. End Enum
  9.  
  10. Public Enum ChatMsgStatus
  11. Undefined ' kludge to force the initial state
  12. Unknown
  13. [New]
  14. Read
  15. End Enum
  16.  
  17. ' one set of images for all chatter instances
  18. Private Shared Imgs As Image()
  19.  
  20. Public Property ChatId As Int32 ' or guid?
  21.  
  22. Private chName As String = ""
  23. Public ReadOnly Property ChatName As String
  24. Get
  25. Return chName
  26. End Get
  27. End Property
  28.  
  29. Private mStatus As ChatMsgStatus = ChatMsgStatus.Undefined
  30. Public Property MsgStatus As ChatMsgStatus
  31. Get
  32. Return mStatus
  33. End Get
  34. Set(value As ChatMsgStatus)
  35. If (value <> mStatus) Then
  36. Select Case value
  37. Case ChatMsgStatus.New
  38. pbMStatus.Image = Imgs(3)
  39. Case ChatMsgStatus.Read
  40. pbMStatus.Image = Imgs(4)
  41. Case Else
  42. pbMStatus.Image = Imgs(4)
  43. End Select
  44. End If
  45. mStatus = value
  46. End Set
  47. End Property
  48.  
  49. Private chStatus As ChatStatus = ChatStatus.Unknown
  50. Public Property Status As ChatStatus
  51. Get
  52. Return chStatus
  53. End Get
  54. Set(value As ChatStatus)
  55. If value <> chStatus Then
  56. Select Case value
  57. Case ChatStatus.Online
  58. pbUStatus.Image = Imgs(0)
  59. Case ChatStatus.Away
  60. pbUStatus.Image = Imgs(1)
  61. Case ChatStatus.Offline
  62. pbUStatus.Image = Imgs(2)
  63. Case Else
  64. End Select
  65. End If
  66. chStatus = value
  67. End Set
  68. End Property
  69.  
  70. Public Sub New()
  71. ' This call is required by the designer.
  72. InitializeComponent()
  73.  
  74. If Imgs Is Nothing Then
  75. Imgs = New Image() {My.Resources.ChatUserGrn,My.Resources.ChatUserYlw,My.Resources.ChatUserRed,My.Resources.ChatBalloonGrn,My.Resources.ChatBalloonGry}
  76.  
  77. ' see note
  78. End If
  79. End If
  80. ' Add any initialization after the InitializeComponent() call.
  81. End Sub
  82. ' no need to create one without Identifiers
  83. Public Sub New(n As Int32,cname As String)
  84. MyClass.New()
  85. ' default intitial values:
  86. chName = cname
  87. ChatId = n
  88.  
  89. lblChName.Text = cname
  90. Me.Status = ChatStatus.Online
  91. Me.MsgStatus = ChatMsgStatus.Unknown
  92. End Sub
  93. End Class

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

我相信还有更多内容,我可以想到一些我希望它知道的事情(比如用未读消息的数量覆盖绿色气球).但这里的重点是封装,DRY和可重用代码的概念.

编译时,工具箱中会有一个新的Chatter控件.使用公开的属性在运行时添加一些:

  1. Dim c As New Chatter(42,"Ziggy von Hausen")
  2. flpChat.Controls.Add(c)
  3.  
  4. c = New Chatter(14,"ThDutoit")
  5. c.MsgStatus = Chatter.ChatMsgStatus.New
  6. flpChat.Controls.Add(c)
  7.  
  8. c = New Chatter(78,"Plutonix")
  9. c.Status = Chatter.ChatStatus.Offline
  10. flpChat.Controls.Add(c)
  11.  
  12. c = New Chatter(4,"Codexer")
  13. c.MsgStatus = Chatter.ChatMsgStatus.New
  14. c.Status = Chatter.ChatStatus.Away
  15. flpChat.Controls.Add(c)

Id将是唯一标识每个聊天参与者的东西.该名称通常不够(SO已超过50 pages of people named “Steve”),您将需要一种方法来识别将控件链接用户. (另一种方法是,用户列表中的ChatterBox引用是对相关UserControl的引用:

  1. Dim user = "Codexer"
  2.  
  3. Dim chatter = flpChat.Controls.
  4. OfType(Of Chatter).
  5. FirstOrDefault(Function(c) c.ChatName.StartsWith(user))
  6. If chatter IsNot Nothing Then
  7. chatter.Status = chatter.ChatStatus.Online
  8. End If

每次搜索都是次优的,并且Id会比仅仅名称更好.理想情况是ChatUser类包含应用程序必须由用户存储的所有其他内容.该类应包含对控件的引用,以便在状态发生变化时或类似情况下,类可以简单地:

  1. myChatterBox.Status = myStatus

结果:

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

作为一个额外的好处,因为您不再创建单独的控件,并且因为UserControl继承自Component,所以如果/何时删除它们,您不必担心泄漏.

必读:
Creating a Windows Form User Control

猜你在找的VB相关文章