none
Powershell GUI: How to get selected items from listview box RRS feed

  • Question

  • Hi,

    I'm trying to click on a row in a listview box that I have created in a windows form and get the value in each column then store each value in a variable. But when I try it only gives me the value of the first column. 

    I made a short example below.

    Here is the example script:

    Function Resize-Columns{
    	$ColWidth = (($ListViewBox.Width / ($ListViewBox.Columns).Count) - 1)
    	$ListViewBox.Columns | %{$_.Width = $ColWidth}
    }
    
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    [Windows.Forms.Application]::EnableVisualStyles()
    
    $Mainform        = New-Object System.Windows.Forms.Form
    $Mainform.Height = 225
    $Mainform.Width  = 336
    $Mainform.StartPosition = 'CenterScreen'
    $Mainform.Text = "Example"
    
    $ListViewBox = New-Object System.Windows.Forms.ListView
    $ListViewBox.Location = "10,20"
    $ListViewBox.Size     = "300,150"
    $ListViewBox.Anchor = 'Top, Bottom, Left, Right'
    $ListViewBox.View = 'Details'
    $ListViewBox.GridLines = $true
    $ListViewBox.FullRowSelect = $True
    $ListViewBox.MultiSelect = $False
    $ListViewBox.Add_click({
        foreach($item in $ListViewBox.SelectedItems)
        { 
           $Data = $Item.text
           Write-Host "$Data"
        }
    })
    $Mainform.Controls.Add($ListViewBox)
    
        "Manufacturer", "SerialNumber" | ForEach-Object{[Void]$ListViewBox.Columns.Add($_)}
        Resize-Columns
    
    	$Col0 = $ListViewBox.Columns[0].Text
    
    	$Info = Get-WmiObject -class "Win32_BIOS" |Select-Object Manufacturer, SerialNumber
    
    	$Info | ForEach-Object{
    		$Item = New-Object System.Windows.Forms.ListViewItem($_.(($Col0).ToLower()))
    		ForEach ($Col in ($ListViewBox.Columns| Where-Object{$_.Index -ne 0})){
    			$Field = $Col.Text
    			[String]$SubItem = $_.(($Field).ToLower())
    			If($SubItem -ne $null){
    				[Void]$Item.SubItems.Add($SubItem)
    			}
    			Else{
    				[Void]$Item.SubItems.Add("")
    			}
    		}
    			[void]$ListViewBox.Items.Add($Item)
    	}
    
    [void]$Mainform.ShowDialog()

    I need to get the values of both columns or as many columns as I need.

    I know I need to iterate through it some how but not sure. I've tried so many different things but the below is the only way I've been able to get anything to return and when it does it only does the first columns value.

    The section I'm having trouble with is:

    $ListViewBox.Add_click({
        foreach($item in $ListViewBox.SelectedItems)
        {
           $Data = $Item.text
           Write-Host "$Data"
        }
    })

    The only value I get is: Hewlett-Packard

    If anyone can assist me in figuring out how to get the other values, I would really appreciate it! I know its something simple i'm missing it always is...

    FYI my environment is:

    Win10 x64

    .Net Framework 4.5

    WMF version: 5

    Sunday, March 25, 2018 11:24 PM

Answers

  • You have to get the item and then get al of its subitem's  values. You can only get the values in an event or after the from is closed.

    $ListViewBox.Add_click({
         foreach($item in $ListViewBox.SelectedItems){ 
            Write-Host $Item.text
            foreach($i in $item.SubItems){
                Write-Host $i.Text
            }
         }
     })


    \_(ツ)_/

    Sunday, March 25, 2018 11:40 PM

All replies