vb.net处理picturebox并重新加载
嘿,我的win表单上有大约30个图片框。每次播放新歌时,它都会填充图像(可以将其视为“先前播放的列表”) 表格上的图片框看起来是这样的(3行,共10行) 以下是我在表单上填充图片框的方式:vb.net处理picturebox并重新加载,vb.net,winforms,controls,dispose,Vb.net,Winforms,Controls,Dispose,嘿,我的win表单上有大约30个图片框。每次播放新歌时,它都会填充图像(可以将其视为“先前播放的列表”) 表格上的图片框看起来是这样的(3行,共10行) 以下是我在表单上填充图片框的方式: Private Sub getAlbumArt() ...lots of code in here If lblArtist.Text <> prevArtiest Then prevArtiest = lblArtist.Text AddHand
Private Sub getAlbumArt()
...lots of code in here
If lblArtist.Text <> prevArtiest Then
prevArtiest = lblArtist.Text
AddHandler clearPrevPlayed.DoWork, AddressOf clearPrevPlayed_DoWork
AddHandler clearPrevPlayed.RunWorkerCompleted, AddressOf clearPrevPlayed_RunWorkerCompleted
clearPrevPlayed.RunWorkerAsync()
End If
End Sub
Private Sub clearPrevPlayed_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
For Each ctlControl In Me.Controls
If TypeName(ctlControl) = "PictureBox" Then
If InStr(ctlControl.name, "previous_") <> 0 Then
ctlControl.Image = Nothing
ctlControl.Controls.Clear()
ctlControl.dispose()
End If
If InStr(ctlControl.name, "previousSong_") <> 0 Then
ctlControl.Image = Nothing
ctlControl.Controls.Clear()
ctlControl.dispose()
End If
End If
Application.DoEvents()
Next ctlControl
End Sub
Private Sub clearPrevPlayed_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
Call buildPrePlayed(lblArtist.Text)
End Sub
Private Sub buildPrePlayed(ByVal theArtest As String)
Dim intX As Integer = 0
Dim tmpNewImg(30) As PictureBox
Dim tmpNewImgPlaceholder(30) As PictureBox
Dim thePicsNames(30) As String
Dim filePaths As Linq.IOrderedEnumerable(Of IO.FileInfo) = New DirectoryInfo(Application.StartupPath & "\cdcovers").GetFiles().OrderByDescending(Function(f As FileInfo) f.LastWriteTime)
For Each fi As IO.FileInfo In filePaths
If InStr(fi.Name, "_small") <> 0 And intX <= 30 Then
thePicsNames(intX) = (fi.Name)
intX += 1
End If
Next
intX = 0
Do Until intX = 30
Dim newImgPlaceholder As New PictureBox
Dim newImg As New PictureBox
If thePicsNames(intX) <> "" Then
newImgPlaceholder.Visible = True
newImgPlaceholder.Image = My.Resources.cdPlaceHolder
Else
newImgPlaceholder.Image = My.Resources.BLANK
newImgPlaceholder.Visible = False
End If
newImgPlaceholder.Size = New System.Drawing.Size(130, 130)
newImgPlaceholder.BorderStyle = BorderStyle.None
If intX = 0 Then
newImgPlaceholder.Location = New Point(145, 600)
ElseIf intX >= 1 And intX < 10 Then
newImgPlaceholder.Location = New Point((tmpNewImgPlaceholder(intX - 1).Left + 160), 600)
ElseIf intX = 10 Then
newImgPlaceholder.Location = New Point(145, 760)
ElseIf intX >= 10 And intX <= 19 Then
newImgPlaceholder.Location = New Point((tmpNewImgPlaceholder(intX - 1).Left + 160), 760)
ElseIf intX = 20 Then
newImgPlaceholder.Location = New Point(145, 920)
ElseIf intX >= 21 And intX <= 29 Then
newImgPlaceholder.Location = New Point((tmpNewImgPlaceholder(intX - 1).Left + 160), 920)
End If
newImgPlaceholder.BackColor = Color.Transparent
newImgPlaceholder.Name = "previous_" & intX
If thePicsNames(intX) <> "" Then
newImg.Visible = True
newImg.Image = Image.FromFile(Application.StartupPath & "\cdcovers\" & thePicsNames(intX))
Else
newImg.Image = My.Resources.BLANK
newImg.Visible = False
End If
newImg.Size = New System.Drawing.Size(120, 120)
newImg.BorderStyle = BorderStyle.None
If intX = 0 Then
newImg.Location = New Point(150, 605)
ElseIf intX >= 1 And intX < 10 Then
newImg.Location = New Point((tmpNewImg(intX - 1).Left + 160), 605)
ElseIf intX = 10 Then
newImg.Location = New Point(150, 765)
ElseIf intX >= 10 And intX <= 19 Then
newImg.Location = New Point((tmpNewImg(intX - 1).Left + 160), 765)
ElseIf intX = 20 Then
newImg.Location = New Point(150, 925)
ElseIf intX >= 21 And intX <= 29 Then
newImg.Location = New Point((tmpNewImg(intX - 1).Left + 160), 925)
End If
newImg.BringToFront()
newImg.Name = "previousSong_" & intX
Me.Controls.Add(newImg)
Me.Controls.Add(newImgPlaceholder)
tmpNewImg(intX) = newImg
tmpNewImgPlaceholder(intX) = newImgPlaceholder
intX += 1
Loop
RemoveHandler clearPrevPlayed.DoWork, AddressOf clearPrevPlayed_DoWork
RemoveHandler clearPrevPlayed.RunWorkerCompleted, AddressOf clearPrevPlayed_RunWorkerCompleted
End Sub
私有子getAlbumArt()
…这里有很多代码
如果是lblArtist.Text PREVATIEST,则
prevariest=lblArtist.Text
AddHandler clearPrevPlayed.DoWork,clearPrevPlayed\u DoWork的地址
AddHandler clearPrevPlayed.RunWorkerCompleted,clearPrevPlayed\u RunWorkerCompleted的地址
clearPrevPlayed.RunWorkerAsync()
如果结束
端接头
私有子ClearPrevU DoWork(ByVal发送方作为对象,ByVal e作为DoWorkEventArgs)
对于Me.Controls中的每个控件
如果TypeName(ctlControl)=“PictureBox”,则
如果InStr(ctlControl.name,“previous_uu”)为0,则
ctlControl.Image=Nothing
ctlControl.Controls.Clear()
ctlControl.dispose()
如果结束
如果InStr(ctlControl.name,“previousSong_uuz”)为0,则
ctlControl.Image=Nothing
ctlControl.Controls.Clear()
ctlControl.dispose()
如果结束
如果结束
Application.DoEvents()
下一个CTL控件
端接头
私有子clearPrevPlayed\u RunWorkerCompleted(ByVal发送方作为对象,ByVal e作为RunWorkerCompletedEventArgs)
调用buildPrePlayed(lblArtist.Text)
端接头
预显示的私有子构建(ByVal theArtest作为字符串)
Dim intX为整数=0
将tmpNewImg(30)调暗为图片框
将TMPNEWIMG占位符(30)调暗为PictureBox
将图片名称(30)设置为字符串
Dim filepath As Linq.IOrderedEnumerable(Of IO.FileInfo)=New DirectoryInfo(Application.StartupPath&“\cdcovers”).GetFiles().OrderByDescending(函数(f As FileInfo)f.LastWriteTime)
对于每个fi,作为文件路径中的IO.FileInfo
如果InStr(fi.Name,“_small”)0且intX=1且intX<10,则
newImgPlaceholder.Location=新点((tmpnewimg占位符(intX-1)。左+160),600)
ElseIf intX=10,则
newImgPlaceholder.Location=新点(145760)
如果intX>=10且intX=21且intX=1且intX<10,则
newImg.Location=新点((tmpNewImg(intX-1)。左+160),605)
ElseIf intX=10,则
新建位置=新点(150765)
ElseIf intX>=10、intX=21和intX只需在表单级别保留一个列表(PictureBox),并在创建它们时将它们添加到列表中。现在,您可以迭代该列表以除去它们,而无需再次“查找”它们
所以它可以看起来像这样:
Private Previous As New List(Of PictureBox)
Private Sub getAlbumArt()
' ...lots of code in here
If lblArtist.Text <> prevArtiest Then
prevArtiest = lblArtist.Text
For Each pb As PictureBox In Previous
pb.Dispose()
Next
Previous.Clear()
Application.DoEvents()
buildPrePlayed(lblArtist.Text)
End If
End Sub
Private Sub buildPrePlayed(ByVal theArtest As String)
' ...lots of code in here
intX = 0
Do Until intX = 30
Dim newImgPlaceholder As New PictureBox
Dim newImg As New PictureBox
Previous.Add(newImgPlaceholder)
Previous.Add(newImg)
' ...lots of code in here
Loop
End Sub
Private Previous As New List(图片盒)
私有子对象
“…这里有很多代码
如果是lblArtist.Text PREVATIEST,则
prevariest=lblArtist.Text
对于前一页中的每个pb作为PictureBox
pb.Dispose()
下一个
上一篇
Application.DoEvents()
buildPrePlayed(lblArtist.Text)
如果结束
端接头
预显示的私有子构建(ByVal theArtest作为字符串)
“…这里有很多代码
intX=0
直到intX=30为止
将新ImgPlaceHolder调暗为新图片盒
将新图片变暗为新图片框
上一个。添加(新imgplaceholder)
上一个。添加(新img)
“…这里有很多代码
环
端接头
这不起作用。这些图像现在不会显示在表单上。没有错误。我没有添加任何阻止它们显示的代码。唯一需要删除(或注释掉)的内容是与BackgroundWorker()控件相关的内容。其他一切都应该与我上面概述的少量更改相同。有可能我遗漏了什么……因为代码与应用程序中的流程有关,所以很难理解代码。
If InStr(ctlControl.name, "previous_") <> 0 Then
ctlControl.Image = Nothing
ctlControl.Controls.Clear()
ctlControl.dispose()
End If
Private Previous As New List(Of PictureBox)
Private Sub getAlbumArt()
' ...lots of code in here
If lblArtist.Text <> prevArtiest Then
prevArtiest = lblArtist.Text
For Each pb As PictureBox In Previous
pb.Dispose()
Next
Previous.Clear()
Application.DoEvents()
buildPrePlayed(lblArtist.Text)
End If
End Sub
Private Sub buildPrePlayed(ByVal theArtest As String)
' ...lots of code in here
intX = 0
Do Until intX = 30
Dim newImgPlaceholder As New PictureBox
Dim newImg As New PictureBox
Previous.Add(newImgPlaceholder)
Previous.Add(newImg)
' ...lots of code in here
Loop
End Sub