【发布时间】:2014-01-13 15:42:05
【问题描述】:
我正在使用 Visual Studio 2010 并在 VB 中进行编码。
我正在获取文件夹中的目录以填充 ListView,并且这样做没有问题。 我还可以通过在文件夹中搜索文件来将图像添加到 ImageList。 我不能做的就是将两者联系起来。如果我硬编码我的项目并硬编码我的图像,它们就可以正常工作。
我还需要弄清楚如何为每个项目添加标签。标签应该是目录的名称。 (来自第一个 For Each 的目录)
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set ListView view mode to show Large Icons
ListView1.View = View.LargeIcon
Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
Dim dir As String
For Each dir In dirs
dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
ListView1.Items.Add(New ListViewItem(dir))
Next
'Create ImageList objects.
Dim imageListLarge As New ImageList()
imageListLarge.ImageSize = New Size(100, 100)
Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
Dim files As String
For Each files In filesList
'files = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
imageListLarge.Images.Add(Bitmap.FromFile(files))
Next
'Assign the ImageList objects to the ListView.
ListView1.LargeImageList = imageListLarge
End Sub
这是相同的代码,但对信息进行了硬编码。 下一个示例完全按照我想要的方式工作。 唯一的问题是有些客户可能拥有全部 6 件物品,而有些客户可能没有。 这就是需要 For Each 的原因。
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set ListView view mode to show Large Icons
ListView1.View = View.LargeIcon
' Create items for the listview
Dim item1 As New ListViewItem("Item 1", 5)
Dim item2 As New ListViewItem("Item 2", 4)
Dim item3 As New ListViewItem("Item 3", 0)
Dim item4 As New ListViewItem("Item 4", 3)
Dim item5 As New ListViewItem("Item 5", 1)
Dim item6 As New ListViewItem("Item 6", 2)
'Set the group for the items
item1.Group = ListView1.Groups("IETMs")
item2.Group = ListView1.Groups("IETMs")
item3.Group = ListView1.Groups("IETMs")
item4.Group = ListView1.Groups("IETMs")
item5.Group = ListView1.Groups("IETMs")
item6.Group = ListView1.Groups("IETMs")
'Set a tag for the items
item1.Tag = "This is file 1"
item2.Tag = "This is file 2"
item3.Tag = "This is file 3"
item4.Tag = "This is file 4"
item5.Tag = "This is file 5"
item6.Tag = "This is file 6"
'Add the items to the ListView.
ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3, item4, item5, item6})
'Create ImageList objects.
Dim imageListLarge As New ImageList()
imageListLarge.ImageSize = New Size(100, 100)
' Initialize the ImageList objects with bitmaps
Dim image1 = Bitmap.FromFile(filePath & "Resources\IETMS\file1.ico")
Dim image2 = Bitmap.FromFile(filePath & "Resources\IETMS\file2.ico")
Dim image3 = Bitmap.FromFile(filePath & "Resources\IETMS\file3.ico")
Dim image4 = Bitmap.FromFile(filePath & "Resources\IETMS\file4.ico")
Dim image5 = Bitmap.FromFile(filePath & "Resources\IETMS\file5.ico")
Dim image6 = Bitmap.FromFile(filePath & "Resources\IETMS\file6.ico")
imageListLarge.Images.AddRange({image1, image2, image3, image4, image5, image6})
'Assign the ImageList objects to the ListView.
ListView1.LargeImageList = imageListLarge
End Sub
这是我修改后的代码。
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set ListView view mode to show Large Icons
ListView1.View = View.LargeIcon
Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
Dim dir As String
For Each dir In dirs
dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
ListView1.Items.Add(New ListViewItem(dir, dir))
Next
'Create ImageList objects.
Dim imageListLarge As New ImageList()
imageListLarge.ImageSize = New Size(100, 100)
Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
Dim files As String
Dim files2 As String
For Each files In filesList
files2 = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
files2 = files2.Replace(".ico", "")
imageListLarge.Images.Add(files2, Bitmap.FromFile(files))
Next
'Assign the ImageList objects to the ListView.
ListView1.LargeImageList = imageListLarge
End Sub
【问题讨论】: