我正在使用Visual Studio 2010并在VB中编码。
我在一个文件夹中获取目录以填充ListView,并且没有问题这样做。 我还可以通过在文件夹中搜索文件来将图像添加到ImageList。 我不能做的就是连接两者。如果我硬编码我的项目并硬编码我的图像,他们工作得很好。
我还需要弄清楚如何为每个项目添加标签。标记应该是目录的名称。 (来自第一个的每个人)
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
答案 0 :(得分:1)
ImageList集合项也可以使用一键来轻松查找:
imageListLarge.Images.Add(files, Bitmap.FromFile(files)))
然后你可以用钥匙来引用它们:
Dim bmp As Bitmap = imageListLarge.Images("my file name")
添加ListView项目时,可以使用ImageKey作为参考:
Dim item1 As New ListViewItem("2J-F108-100", "2J-F108-100")
这假设您使用相同的键添加了图像:
imageListLarge.Images.Add("2J-F108-100", Bitmap.FromFile(...)))
此外,请确保在添加ListItems之前添加图像:
Dim imageListLarge As New ImageList()
imageListLarge.ImageSize = New Size(100, 100)
Dim filesList As String() = Directory.GetFiles(filePath & "Resources\IETMS\")
Dim files2 As String
For Each files As String In filesList
files2 = files.Replace(files.Substring(0, files.LastIndexOf("\") + 1), "")
files2 = files2.Replace(".ico", "")
imageListLarge.Images.Add(files2, Bitmap.FromFile(files))
Next
ListView1.LargeImageList = imageListLarge
ListView1.View = View.LargeIcon
Dim dirs As String() = Directory.GetDirectories(filePath & "Resources\IETMS\")
For Each dir As String In dirs
dir = dir.Replace(dir.Substring(0, dir.LastIndexOf("\") + 1), "")
ListView1.Items.Add(New ListViewItem(dir, dir))
Next