如何让我的listview显示图片,我的代码出了什么问题?
Dim lines As String() = listaa.Split(",")
lines = lines.Where(Function(s) s.Trim() <> String.Empty).ToArray()
For Each item In lines
Next
ListView1.Items.Clear()
Dim imageListSmall As New ImageList()
Dim imageListLarge As New ImageList()
imageListSmall.ImageSize = New Size(80, 80)
imageListLarge.ImageSize = New Size(110, 110)
For Each line In lines
Dim item1 As New ListViewItem(line, 1)
ListView1.Items.AddRange(New ListViewItem() {item1})
'dsadsadsa
Dim lool$ = line.IndexOf("watch?v=") + 8
Dim lengg$ = line.Length - lool
Dim tClient As WebClient = New WebClient
Dim tImage As Bitmap = Bitmap.FromStream(New MemoryStream(tClient.DownloadData("http://i1.ytimg.com/vi/" & line.ToString.Substring(lool, lengg).Trim & "/hqdefault.jpg")))
imageListSmall.Images.Add(line.ToString.Substring(lool, lengg).Trim, tImage)
imageListSmall.Images.Add(line.ToString.Substring(lool, lengg).Trim, tImage)
ListView1.LargeImageList = imageListLarge
ListView1.SmallImageList = imageListSmall
Next
Me.Controls.Add(ListView1)
ListView1.Columns.Add("Video found", -2, HorizontalAlignment.Left)
那么,我的代码又出了什么问题?为什么图像不会出现在listview1中,而只出现在文本
中 ListView1.View = View.LargeIcon
ListView1.AllowColumnReorder = False
ListView1.GridLines = False
ListView1.Sorting = SortOrder.Ascending
答案 0 :(得分:0)
要显示图像的每个ListViewItem需要设置ImageKey
或ImageIndex`以指示ImageList中的哪个图像与之相关:
item1.ImageIndex = 1
' or
item1.ImageKey = "Video"
您只需要设置ImageList一次(您的代码在循环中反复执行)。
在构建ImageList时,您可以在添加图像的同时分配密钥:
imgList.Images.Add("Fruit", My.Resources.Fruit)
imgList.Images.Add("Veggie", My.Resources.Veggie)
' then assign to an item:
newItem.ImageKey = "Fruit"
由于您是从网站解析此问题,因此您可能需要在结尾处进行检查,以查看是否添加了任何图像。如果您只是使用索引来启动(Item1.ImageIndex = someVar
),您将能够看到是否有任何索引被提取到ImageList中。