vb.net 在 Visual Basic 中在运行时将图像添加到 ImageList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21095324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add images to ImageList at runtime in Visual Basic
提问by xRuhRohx
I am using Visual Studio 2010 and coding in VB.
我正在使用 Visual Studio 2010 并在 VB 中编码。
I am getting the directories in a folder to populate a ListView and have no issues doing that. I can also add images to an ImageList by searching through the folder for the files. The thing I cannot do is connect the two. If I hard code my items in and hard code my images in, they work just fine.
我正在获取文件夹中的目录以填充 ListView 并且这样做没有问题。我还可以通过在文件夹中搜索文件来将图像添加到 ImageList。我不能做的是将两者联系起来。如果我硬编码我的项目并硬编码我的图像,它们就可以正常工作。
I also need to figure out how to add a Tag for each item. The tag should be the name of the directory. (dir from the first For Each)
我还需要弄清楚如何为每个项目添加一个标签。标签应该是目录的名称。(来自第一个 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
Here is the same code but hard coding the info. This next example works exactly how I want it to. Only problem is Some clients might have all 6 items and some might not. Thats the reason for needing the For Each.
这是相同的代码,但对信息进行了硬编码。下一个示例完全按照我的意愿工作。唯一的问题是有些客户可能拥有全部 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
Here is my modified code.
这是我修改后的代码。
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
采纳答案by LarsTech
The ImageList collection items also can take a key for easy look-up:
ImageList 集合项也可以带一个键以便于查找:
imageListLarge.Images.Add(files, Bitmap.FromFile(files)))
Then you can reference them by the key:
然后你可以通过键来引用它们:
Dim bmp As Bitmap = imageListLarge.Images("my file name")
When you add your ListView item, you can use the ImageKey as the reference:
添加 ListView 项时,可以使用 ImageKey 作为参考:
Dim item1 As New ListViewItem("2J-F108-100", "2J-F108-100")
this assumes you added the image with the same key:
这假设您使用相同的键添加了图像:
imageListLarge.Images.Add("2J-F108-100", Bitmap.FromFile(...)))
Also, make sure to add your images before adding the ListItems:
另外,请确保在添加 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