Skip to content

ListView

In a ScriptoForm, the Microsoft .NET ListView class is a control that is used to display a collection of items represented by icons and/or text. These items can be interactive giving the user the ability to double-click an item to perform an action within the ScriptoForm. Items within a ListView can be divided into separate groups by using a ListViewGroup object to group items by a useful category.

ScriptoForm with example ListView control

In a ScriptoForm PowerShell script file, a ListView control should be instantiated in the Controls region and then defined within the Forms script block. A ListView is not usually paired with a matching Label control, but can be if needed. By convention, a ListView control should be nested in the $GroupBoxMain GroupBox container.

The steps needed to implement a ComboBox control with a ScriptoForm include:

  • Instantiate a ListView object in the Controls region.
    Suggested name format: $ListViewName
  • Instatiate one or more ListViewGroup objects to separate groups of items in the ListView in the Forms region, as needed.
    Suggested name format: $ListViewGroupName
  • Assign the required and any optional properties to the ListBox object in the Forms region.
  • Add the ListView object to a GroupBox object in the Forms region.
  • Define event handlers for the ListView with script blocks in the Handlers region.
  1. Instantiate a new System.Windows.Forms.ListView and System.Windows.Forms.ListViewGroup objects:
    Controls Region
    #region Controls
    $FormMain = New-Object -TypeName System.Windows.Forms.Form
    $GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox
    <#
    Instantiate control objects here...
    #>
    $ListViewItems = New-Object -TypeName System.Windows.Forms.ListView
    $ListViewGroupTools = New-Object -TypeName System.Windows.Forms.ListViewGroup
    $ListViewGroupScripts = New-Object -TypeName System.Windows.Forms.ListViewGroup
    $ButtonRun = New-Object -TypeName System.Windows.Forms.Button
    $ButtonClose = New-Object -TypeName System.Windows.Forms.Button
    $StatusStripMain = New-Object -TypeName System.Windows.Forms.StatusStrip
    $ToolStripStatusLabelMain = New-Object -TypeName System.Windows.Forms.ToolStripStatusLabel
    #endregion
  2. Assign properties and event handler listeners to the ListView object add it to the GroupBox object; assign properties to each ListViewGroup add then add them to the ListView object:
    Forms Region | $ShowMainForm
    #region Forms
    $ShowFormMain =
    {
    $FormWidth = 330
    $FormHeight = 260
    $FormMain.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -Id $PID).Path)
    $FormMain.Text = "Title"
    $FormMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    $FormMain.ClientSize = New-Object -TypeName System.Drawing.Size($FormWidth,$FormHeight)
    $FormMain.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
    $FormMain.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
    $FormMain.MaximizeBox = $false
    $FormMain.AcceptButton = $ButtonRun
    $FormMain.CancelButton = $ButtonClose
    $FormMain.Add_Shown($FormMain_Shown)
    $GroupBoxMain.Location = New-Object -TypeName System.Drawing.Point(10,5)
    $GroupBoxMain.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 20),($FormHeight - 80))
    $FormMain.Controls.Add($GroupBoxMain)
    <#
    Assign control properties and event handlers here and add to the GroupBox...
    #>
    $ListViewItems.Anchor = "Top,Bottom,Left,Right"
    $ListViewItems.MultiSelect = $false
    $ListViewItems.ContextMenuStrip = $ContextMenuStripListView
    $ListViewItems.LabelWrap = $false
    $ListViewItems.LargeImageList = $ImageList
    $ListViewItems.Location = New-Object -TypeName System.Drawing.Point(13,15)
    $ListViewItems.Size = New-Object -TypeName System.Drawing.Size((430 + $FormWidthOffset),(347 + $FormHeightOffset))
    $ListViewItems.Sorting = [System.Windows.Forms.SortOrder]::Ascending
    $ListViewItems.TabIndex = 0
    $ListViewItems.View = [System.Windows.Forms.View]::Tile
    $ListViewItems.Add_DoubleClick($ListViewItems_DoubleClick)
    $ListViewItems.Add_Click($ListViewItems_Click)
    $ListViewItems.Add_MouseDown($ListViewItems_MouseDown)
    $FormMain.Controls.Add($ListViewItems)
    $ListViewGroupTools.Header = "Tools"
    [void]$ListViewItems.Groups.Add($ListViewGroupTools)
    $ListViewGroupScripts.Header = "Scripts"
    [void]$ListViewItems.Groups.Add($ListViewGroupScripts)
    $ButtonRun.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 175),($FormHeight - 60))
    $ButtonRun.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonRun.TabIndex = 100
    $ButtonRun.Enabled = $false
    $ButtonRun.Text = "Run"
    $ButtonRun.Add_Click($ButtonRun_Click)
    $FormMain.Controls.Add($ButtonRun)
    $ButtonClose.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 85),($FormHeight - 60))
    $ButtonClose.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonClose.TabIndex = 101
    $ButtonClose.Text = "Close"
    $ButtonClose.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $FormMain.Controls.Add($ButtonClose)
    $StatusStripMain.SizingGrip = $false
    $StatusStripMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    [void]$StatusStripMain.Items.Add($ToolStripStatusLabelMain)
    $FormMain.Controls.Add($StatusStripMain)
    [void]$FormMain.ShowDialog()
    $FormMain.Dispose()
    }
    #endregion
  3. Create the event handlers for the ListBox object.
    Handlers Region | $ListViewItems_DoubleClick
    $ListViewItems_DoubleClick =
    {
    try
    {
    $ProcessAttributes = @{}
    if ($ListViewItems.SelectedItems.Tag.Argument) {$ProcessAttributes["ArgumentList"] = $ListViewItems.SelectedItems.Tag.Argument}
    if ($ListViewItems.SelectedItems.Tag.WD) {$ProcessAttributes["WorkingDirectory"] = $ListViewItems.SelectedItems.Tag.WD}
    Start-Process -FilePath $ListViewItems.SelectedItems.Tag.Command @ProcessAttributes -ErrorAction Stop
    }
    catch
    {
    [void][System.Windows.Forms.MessageBox]::Show(
    $PSItem.Exception.Message + "`n`nPlease contact $SUPPORT_CONTACT for technical support.",
    "Exception",
    [System.Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Warning
    )
    }
    [gc]::collect()
    }

Starting with Microsoft .NET 5, the ability to collapse/expand a ListView group was added as well as several other useful features.

ListView Class (System.Windows.Forms) | Microsoft Learn