PowerShell In GUI Blog

PowerShell GUI Box Just In A Few Clicks

Archive for the ‘System.Windows.Forms.ListViewItem`+ListViewSubItem’ Category

How to deal with a ListView control

with 4 comments

As the answer to a question asked on the forum, ‘How to populate a ListView Control from an Array?’, I’d like to publish a funciton used to fill up with data the top-right listview in the Object Browser.

The general use of this funciton was to add row of data (data for several columns) independently whether it was the first row (clean up of a control is required) or not.

The funciton provided also supports highlighting being called in a loop (depends on a loop writer).

#region function fillDetailedListView
function fillDetailedListView
{
    <#
        .Synopsis
            Creates columns if they are missing and adds a row of data
        .Description
            This function creates add a row based from a string array. It also optionally clear columns and 
            adds missing columns
        .Parameter ListView
            A reference to a listview control
        .Parameter CleanUp
            An optional flag to clear columns and data before adding a new row
        .Parameter ColumnName
            A string array used as a list of header names
        .Parameter Data
            A string array used as a list of data to be added to a new row
        .Parameter Color
            A flag of the [System.Drawing.SystemColors]::Window type to apply to the newly created row
        .Example
            fillListView ([ref]$lv) $false (,'Name') (,$key) $null; 
        .Notes
            Author: Alexander Petrovskiy
    #>
    param(
        [ref]$ListView,
        [bool]$CleanUp = $false,
        [string[]]$ColumnName,
        [string[]]$Data,
        $Color,
        [int]$ImageIndex = $null,
        [string]$NodeKey = ''
    )
    if ($cleanUp) #if clean-up is required
    {
        ($ListView.Value).Columns.Clear();
    }
    for ($i = 0; $i -lt $ColumnName.Length; $i++)
    {#check whether the Current column exists or not
        if ( -not ([System.Windows.Forms.ListView] `
            ($ListView.Value)).Columns[$ColumnName[$i]])
        {#add only if it's a new one
            ($ListView.Value).Columns.AddRange(
                (($header = New-Object System.Windows.Forms.ColumnHeader) `
                    | %{$header.Text = $ColumnName[$i]; 
                        $header.Name = $ColumnName[$i]; 
                        $header;}));
        }
    }
    if ($Color -eq $null -or `
        $Color.GetType().ToString() -ne 'System.Drawing.SystemColors')
    {#input test of the $Color variable
        $Color = [System.Drawing.SystemColors]::Window;
    }
    #adding items aka rows (an item is a single element of a row,
    #a place where a row and a column are intercrossed
    $listViewItem1 = (($listViewItem = New-Object "System.Windows.Forms.ListViewItem") `
        | %{$listViewItem.Text = $Data[0]; 
            if ($Color -ne [System.Drawing.SystemColors]::Window)
            {#set $Color to all items in the row
                $listViewItem.BackColor = $Color;
                $listViewItem.UseItemStyleForSubItems = $true;
            }
            if ($ImageIndex -ne $null)
            {#if you have an ImageList control in your form
                $listViewItem.ImageIndex = $ImageIndex + 1;
            }
            if ($NodeKey -ne $null -and $NodeKey.Length -gt 0)
            {
                $listViewItem.Tag = $NodeKey;
            }
        #more columns
        for ($i = 1; $i -lt $Data.Length; $i++)
        {#adding data to the row items
            $listViewItem.SubItems.Add((([System.Windows.Forms.ListViewItem`+ListViewSubItem]$subItem = `
                New-Object System.Windows.Forms.ListViewItem`+ListViewSubItem) `
                | %{$subItem.Text = $Data[$i]; 
                    $subItem;}));
        }
        $listViewItem;}
        )
    ($ListView.Value).Items.Add($listViewItem);
    #setting AutoREsize property
    if ($Data -ne $null -and $Data.Length -gt 1)
    {
        ($ListView.Value).AutoResizeColumns([System.Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent);
    }
    else
    {
        ($ListView.Value).AutoResizeColumns([System.Windows.Forms.ColumnHeaderAutoResizeStyle]::HeaderSize);
    }
}
#endregion function fillDetailedListView


#How to use
fillDetailedListView -ListView ([ref]$lvBottom) `
    -CleanUp $false `
    -ColumnName ('Property','Value') `
    -Data ($key,$properties[$key]) `
    -ImageIndex $null `
    -NodeKey $null;

This code sample simply adds a row with items in the columns ‘Property’ and ‘Value’ and doesn’t clean up existing columns.

It’s considered that the ListView you use here called $lvBottom, otherwise you face the following error:
“[ref] cannot be applied to a variable that does not exist.”

A copy of this code snippet has also been posted here: http://powergui.org/thread.jspa?threadID=12497

Advertisements