Page 1 of 1

Using the ListView control Add-ListViewItem helper

Posted: Mon Feb 03, 2014 12:28 pm
by PGomersall
If the add a ListView control to a form a couple of helper items are added.
How would I use the "Add-ListViewItem" to populate the control from say using the following: (get-volume | select -property DriveLetter, FileSystemLabel). Playing around with the helper function I cannot get it to add anything. I am trying to add it via the form load event.
Pete

Re: Using the ListView control Add-ListViewItem helper

Posted: Mon Feb 03, 2014 12:50 pm
by davidc
Depends on what you wish to display and how.

If you want to display the drive letter and label you could try the following:

PowerShell Code
Double-click the code block to select all.
$volumes = get-volume 
foreach($drive in $volumes)
{
	Add-ListViewItem -ListView $listview1 -Items $drive.DriveLetter -SubItems $drive.FileSystemLabel
}
The ListView can be a complicated control. I recommend reading the Spotlight on the ListView control article:

http://www.sapien.com/blog/2012/04/04/s ... ol-part-1/

You can use a DataGridView as an alternative.

If you have further questions about using the GUI controls, we have a forum dedicated to the subject:

PowerShell GUIs

David

Re: Using the ListView control Add-ListViewItem helper

Posted: Mon Feb 03, 2014 3:17 pm
by PGomersall
David,
Tried the code you suggest, but it doesn't return any data to the ListView control.
Pete

Re: Using the ListView control Add-ListViewItem helper

Posted: Mon Feb 03, 2014 5:04 pm
by davidc
I didn't validate any of the fields or names. You will have to make sure you aren't using the wrong variable name.

I recommend running it through the debugger or just output the values.

David