Page 1 of 2
Listview imagenlist icons
Posted: Sat Feb 11, 2012 2:45 pm
by AnoPem
Hello im creating a file shredder and i need to know how i can add the associated icons to the files that are shown in the listview, ive been searching for hours and not really found anything that worked as it should
Would be nice if anyone could help me out here
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 3:26 pm
by Dummy1912
something like this:
make a new imagelist
add a image into it
then select your listview and go to the properties and select smallimagelist
and select the imagelist
Code: Select allDim li As New ListViewItem()
li.SubItems.Add(fi.Name)
li.ImageIndex = 0
Listview1.Items.Add(li)
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 3:54 pm
by MrAksel
Something like this?
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 3:54 pm
by AnoPem
Dummy1912 wrote:something like this:
make a new imagelist
add a image into it
then select your listview and go to the properties and select smallimagelist
and select the imagelist
Code: Select allDim li As New ListViewItem()
li.SubItems.Add(fi.Name)
li.ImageIndex = 0
Listview1.Items.Add(li)
I want it to find the associated icons for like exe, txt etc.
by itself
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 3:56 pm
by AnoPem
MrAksel wrote:Something like this?
it excatly like that but i dont know how i can get it without the treeview
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 4:15 pm
by MrAksel
Do you see the file Icons.vb? You can use that class to get the icon from a file.
Code: Select allDim Icons As New Icons(True) 'True = Big icons, False = small icons
ListView1.LargeImageList = Icons.LargeIcons
Dim item1 As New ListViewItem("file1.txt")
item1.ImageIndex = Icons.GetIconIndex("C:\file1.txt")
ListView1.Items.Add(item1)
If you have a file named 'file1.txt' in your C:\ directory, it will load the correct icon and add it to a ListView named ListView1.
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 4:24 pm
by AnoPem
MrAksel wrote:Do you see the file Icons.vb? You can use that class to get the icon from a file.
Code: Select allDim Icons As New Icons(True) 'True = Big icons, False = small icons
ListView1.LargeImageList = Icons.LargeIcons
Dim item1 As New ListViewItem("file1.txt")
item1.ImageIndex = Icons.GetIconIndex("C:\file1.txt")
ListView1.Items.Add(item1)
If you have a file named 'file1.txt' in your C:\ directory, it will load the correct icon and add it to a ListView named ListView1.
Is it possible when you click a button that have a folderbrowsedialog it will show the icons after the folder has been choosen ? so it will work as the file you posted? just without the treeview
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 4:36 pm
by MrAksel
Code: Select allDim icons As New Icons(True)
ListView1.LargeImageList = icons.LargeIcons
Dim browser As New FolderBrowserDialog
If browser.ShowDialog() = DialogResult.OK Then
For Each f As String In System.IO.Directory.GetFiles(browser.SelectedPath)
Dim Item As New ListViewItem(System.IO.Path.GetFileName(f))
Item.ImageIndex = icons.GetIconIndex(f)
ListView1.Items.Add(Item)
Next
End If
You could have that in a button, as long as you add the class Icons.vb from the file I uploaded to your project. I think it works, but I didn't test it.
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 4:41 pm
by AnoPem
MrAksel wrote:Code: Select allDim icons As New Icons(True)
ListView1.LargeImageList = icons.LargeIcons
Dim browser As New FolderBrowserDialog
If browser.ShowDialog() = DialogResult.OK Then
For Each f As String In System.IO.Directory.GetFiles(browser.SelectedPath)
Dim Item As New ListViewItem(System.IO.Path.GetFileName(f))
Item.ImageIndex = icons.GetIconIndex(f)
ListView1.Items.Add(Item)
Next
End If
You could have that in a button, as long as you add the class Icons.vb from the file I uploaded to your project. I think it works, but I didn't test it.
I added this code to my button but no icons did show up and 2 folderbrowse dialogs showed up
Re: Listview imagenlist icons
Posted: Sat Feb 11, 2012 4:43 pm
by MrAksel
Did it show any text at all? Did you have any other code in your button?