Imagelist with AddRange from a CSV file

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
WSUsoftware
Posts: 30
Last visit: Thu Feb 17, 2022 2:52 pm

Imagelist with AddRange from a CSV file

Post by WSUsoftware »

I am making a form to install software. I am using a ListView that is populated by a CSV file. The "tiles" per say that are visible get the text value from a Name column in the CSV. I want to populate the image part of the tile with icons as well. I have this setup using an ImageList and have figured out how to add 1 individual icon to the ImageList through scripting from the CSV but have not been able to get the ImageList to read more than one item from the CSV.

The methodology I am using is thus:

This is used for multiple different parts:
$apps = Import-CSV 'network path'

Icon is a column in the CSV file where the items in it's column are network paths to the individual .ico files:
$imgs = $apps.Icon

To populate the ImageList with the correct icons in order, i use:
$imagelist1.Images.AddRange(@($imgs))

I would think this would work, but I get the following error when running it:
"Cannot convert the "network path to .ico" value of type "System.String" to type "System.Drawing.Image"

I realize that there are many ways to manually set the icons or to even hard code every individual line, but I really want to avoid that methodology so that it is more dynamic and everything can be edited in one spot, outside of the form.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Imagelist with AddRange from a CSV file

Post by jvierra »

Something like this:
  1. Import-Csv images.csv |
  2.     ForEach-Object{
  3.         $icon = [System.Drawing.Image]::FromFile($_.Icon)
  4.         $imagelist1.Images.Add($_.Name, $icon)
  5.     }
User avatar
WSUsoftware
Posts: 30
Last visit: Thu Feb 17, 2022 2:52 pm

Re: Imagelist with AddRange from a CSV file

Post by WSUsoftware »

I believe that worked. I'm not getting any error anymore. Something I didn't realize until the code you supplied is that with my method for populating the ListView only used the first image from the ImageList to be the icon for all the tiles.

This is the command I used:

Add-ListViewItem -ListView $listview1 -Items $apps.Name -Group $listview1.Groups[0] -ImageIndex 0

I'm guessing maybe there is a different parameter I can use in the Add-ListViewItem? Or should I just use a different method than Add-ListViewItem?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Imagelist with AddRange from a CSV file

Post by jvierra »

$listview1.LargeImageList = $imagelist1

Add-ListViewItem -ListView $listview1 -Items $apps -Group $listview1.Groups[0]

You will have to manually assign the images or add the items one at a time and assign the correct imageindex.

Here is a three part article on how to use the listview so that it works. https://www.sapien.com/blog/2012/04/04/ ... ol-part-1/
User avatar
jazz albert
Posts: 16
Last visit: Sat Nov 05, 2016 3:09 pm

Re: Imagelist with AddRange from a CSV file

Post by jazz albert »

jvierra wrote:$listview1.LargeImageList = $imagelist1

Add-ListViewItem -ListView $listview1 -Items $apps -Group $listview1.Groups[0]

You will have to manually assign the images or add the items one at a time and assign the correct imageindex.

Here is a three part article on how to use the listview so that it works. https://www.sapien.com/blog/2012/04/04/ ... ol-part-1/
It is little complicated for a layman to get it done!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Imagelist with AddRange from a CSV file

Post by jvierra »

I think your missing piece of information is the following: An image is chosen automatically when its key matches the item name in the view.

Here is a short quick example that loads two icons from the system and names them then uses the image key name to select the image to display.
Attachments
Demo-ListViewIcons.psf
(25.94 KiB) Downloaded 180 times
This topic is 7 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked