ADTJOB
ADTJOB

Reputation: 85

powershell gui select list highlight

I hope there i can get an answer for this quick. Im populating a list box based on radio button being selected. I want the list box to highlight the row automatically so that when i click the button the relevant action can happen based on the selection. In this case it will be a sql server instance. See extract of code for adding it to ListBox1.

ForEach ($Server in $Servers)
                    {

                #$NL = "`r`n"

                [void] $ListBox1.Items.Add($Server)
                       #$ListBox1.Items.selectedItem
                }

Upvotes: 1

Views: 1629

Answers (1)

sodawillow
sodawillow

Reputation: 13176

Example:

$form = New-Object System.Windows.Forms.Form

$listbox = New-Object System.Windows.Forms.ListBox

$listbox.SelectionMode = "MultiSimple"

$listbox.Items.Add("item1") | Out-Null
$listbox.Items.Add("item2") | Out-Null
$listbox.Items.Add("item3") | Out-Null
$listbox.Items.Add("item4") | Out-Null

for($i = 0; $i -lt $listbox.Items.Count; $i++) {
    $listbox.SetSelected($i, $true)
}

$form.Controls.Add($listbox)

$form.ShowDialog()

Upvotes: 3

Related Questions