I'm trying to use a powershell GUI to solve a simple problem: we often have to create quality reports for outgoing parts which involves looking through and inserting inspection photos, but we really only need to pick two photos that show the whole part (making seeking out the folder and trying to sort through dozens of photos seem like a silly way of going about it). So I am creating a GUI that will load all the pictures (icon-sized) in a listbox with checkboxes, with the ability to double-click on a picture to open a new window with the full-sized picture if needed. The user can check two images, click okay, and it will return the two images (eventually to another function that will insert them in the proper place without having to manually open an excel file and insert them using excel's GUI). I have all of this fully-functional in my test code. However, when the inspection photos are taken they are always taken in random order, and the reason we need two pictures is we need one of the top of the part and one of the bottom. I was thinking I could just have instructions to check the top image first, then check the bottom image, and I'll be able to sort the checked items in order of which one the user checked first. This is proving to be more difficult than I anticipated, can someone please assist me? So far I have:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$objForm = New-Object System.Windows.Forms.Form
Add-Type -AssemblyName System.Drawing
$objForm.Text = "Outgoing Inspection Images"
$objForm.Height = 800
$objForm.Width = 1500
$imageList = new-Object System.Windows.Forms.ImageList
$imageList.ImageSize = New-Object System.Drawing.Size(256,256) # Size of the pictures
$Testdir = '...Inspection Picturesoutgoing inspection pictures'
$Testpics = Get-ChildItem -Path $TestDir
$Testpics = $Testpics.FullName
for ($i=0; $i -lt $Testpics.Length; $i++){
$bitm=[System.Drawing.Image]::FromFile($testpics[$i])
$Testname = $testpics[$i] -split "\" |Select-Object -Last 1
$imageList.Images.Add($Testname,$bitm)
}
$listView1 = New-Object System.Windows.Forms.ListView
$listView1.View = 'Details'
$listView1.Height = 800
$listView1.Width = 800
$listView1.CheckBoxes = $true;
$ListView1.MultiSelect = $false;
$listview1.FullRowSelect = $true
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$listView1.Location = $System_Drawing_Point
$listView1.Name = "listView1"
$listView1.Columns.Add('Inspection Picture',400)| Out-Null
for ($i=0; $i -lt $Testpics.Length; $i++){
$Testname = $testpics[$i] -split "\" |Select-Object -Last 1
$listView1.SmallImageList = $imageList
$listView1.Items.Add($Testname,$i)
}
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '850,200'
$OKButton.Size = '100,40'
$OKButton.Text = 'OK'
#Add a cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '1050,200'
$CancelButton.Size = '100,40'
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$objForm.Controls.add($listView1)
$objForm.Controls.add($OKButton)
$OKButton.Add_Click{
$x = $listView1.SelectedItems.Text
write-host "Selected: "
write-host $x
$y = $listview1.CheckedItems.Text
write-host "checked: "
write-host $y
if ($y.count -ne 2){
write-warning -Message "Please check two images"
}
if ($y.count -eq 2){
$Objform.DialogResult=[System.Windows.Forms.DialogResult]::OK
$listView1.Dispose()
$imageList.Dispose()
$objForm.Dispose()
return $y
}
}
$objForm.Controls.add($cancelButton)
Function DoSomething()
{
if($listview1.SelectedItems.Count -eq 1)
{
$file = (-join($testdir, '/', $listview1.SelectedItems[0].Text))
$panel1 = New-Object Windows.Forms.Panel
$Panel1.AutoScroll = $true;
$win = New-Object Windows.Forms.Form
$box = New-Object Windows.Forms.PictureBox
$box.Image = [System.Drawing.Image]::FromFile($file)
$box.AutoSize = $true
$Panel1.controls.add($box)
$win.width = 1500
$win.height = 800
$Win.Text = $listview1.SelectedItems[0].Text
$panel1.width = ($win.width - 15)
$panel1.height = ($win.height -50)
$win.Controls.Add($panel1)
$win.ShowDialog()
}
}
$listview1.Add_ItemActivate({DoSomething})
$result = $objForm.ShowDialog()
The problem is if I check for example "IMG_4949.JPG" and "IMG_4951.JPG", $y seems to always contain them sorted by name. I want to change this so that if I check the box by IMG_4951 first, this will be listed first in the output. Is there any way to achieve this?
question from:
https://stackoverflow.com/questions/65853971/sort-listview-checked-items-by-order-in-which-they-were-checked 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…