Home > database >  Powershell listbox items are sorted wrong
Powershell listbox items are sorted wrong

Time:04-12

The following code gives a listbox where the items are sorted and are sorted wrong. The items should be in the order they are presented in the collection. $dag.Sorted = $false clearly doesn't work here. Does someone have a sollution ? The item names are in Dutch

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$windows_height = 450
$window_width = 600
$form = New-Object System.Windows.Forms.Form
$form.text = ''
$form.Size = New-Object System.Drawing.Size($window_width, $windows_height)
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true

$dag_hash=@{
  Maandag = 2
  Dinsdag = 3
  Woensdag = 4
  Donderdag = 5
  Vrijdag = 6
}

$dag = New-Object System.Windows.Forms.listbox
$dag.Location = '0, 270'
$dag.Size = '250, 70'
$dag.Sorted = $false
$range = $dag_hash.keys
[void]$dag.Items.AddRange($range)
$dag.SetSelected(0,$true)
$form.Controls.Add($dag)

$result = $form.ShowDialog()

enter image description here

CodePudding user response:

As @Theo suggested ad a odered attribute to your hashtable definition:

$dag_hash=[ordered] @{
  Maandag = 2
  Dinsdag = 3
  Woensdag = 4
  Donderdag = 5
  Vrijdag = 6
}
  • Related