求助:PowerShell WinForms为CheckedListBox实现上下移动按钮功能
Hey Graham, let's get that Move Up/Move Down functionality working for your CheckedListBox. Translating C# WinForms code to PowerShell can feel like a puzzle at first, but we'll break this down clearly.
First, let's fix the Move Up button functionality, then add the Move Down button since you'll likely need both for priority adjustment. The key thing to remember with CheckedListBox is that when you move items, you have to preserve their checked state manually—they don't automatically follow the item to its new position.
Step 1: Implement Move Up Functionality
Add this click event handler to your existing $TAB2LISTMU button. This code will:
- Get the currently selected index
- Verify it's not the first item (can't move up from position 0)
- Save the checked state of the item being moved
- Swap the item with the one above it
- Restore the checked state
- Re-select the item in its new position
$TAB2LISTMU.Add_Click({ # Get the selected index of the CheckedListBox $selectedIndex = $TAB2LISTBOX.SelectedIndex # Only proceed if an item is selected and it's not the first one if ($selectedIndex -gt 0) { # Save the checked state of the item we're moving $isChecked = $TAB2LISTBOX.GetItemChecked($selectedIndex) # Get the item itself $itemToMove = $TAB2LISTBOX.Items[$selectedIndex] # Remove the item from its current position $TAB2LISTBOX.Items.RemoveAt($selectedIndex) # Insert it one position up $TAB2LISTBOX.Items.Insert($selectedIndex - 1, $itemToMove) # Restore the checked state $TAB2LISTBOX.SetItemChecked($selectedIndex - 1, $isChecked) # Re-select the item in its new position $TAB2LISTBOX.SelectedIndex = $selectedIndex - 1 } })
Step 2: Add and Implement Move Down Button
Let's add the Move Down button and its functionality too. Just place this code right after your Move Up button setup:
# Add Move Down button $TAB2LISTMD = New-Object System.Windows.Forms.Button $TAB2LISTMD.Location = '530,290' $TAB2LISTMD.Text = "&Move Down" $TAB2.Controls.Add($TAB2LISTMD) # Move Down click event handler $TAB2LISTMD.Add_Click({ $selectedIndex = $TAB2LISTBOX.SelectedIndex # Only proceed if an item is selected and it's not the last one if ($selectedIndex -ne -1 -and $selectedIndex -lt ($TAB2LISTBOX.Items.Count - 1)) { $isChecked = $TAB2LISTBOX.GetItemChecked($selectedIndex) $itemToMove = $TAB2LISTBOX.Items[$selectedIndex] $TAB2LISTBOX.Items.RemoveAt($selectedIndex) $TAB2LISTBOX.Items.Insert($selectedIndex + 1, $itemToMove) $TAB2LISTBOX.SetItemChecked($selectedIndex + 1, $isChecked) $TAB2LISTBOX.SelectedIndex = $selectedIndex + 1 } })
Quick Fix for Your ImportUsers Function
I noticed a small bug in your ImportUsers function—you're using $_ instead of $USER when building the SIP address. That would cause incorrect SIPs (it would reuse the last value from the loop). Fix it like this:
Function ImportUsers { $USERS = gc $TAB2IMPORT $SIPUSERS = @() foreach ( $USER in $USERS ) { # Changed $_ to $USER here $USERSIP = "sip:" + $USER $SIPUSERS += $USERSIP IF ( $USERSIP -ne $NULL ) { [void]$TAB2LISTBOX.Items.Add( $USERSIP ) } IF ( $SIPUSERS.Count -ge 2 ) { $TAB2LISTL.Visible = $TRUE } } }
Note on PowerShell Pro Tools
Since you have PowerShell Pro Tools, their WinForms Designer is perfect for this kind of work—it lets you drag-and-drop controls and auto-generates PowerShell code, which can save you from translating C# manually. If you haven't tried it yet, look for the Forms Designer tool in the Pro Tools menu within Visual Studio or their standalone editor. It's a huge time-saver for WinForms in PowerShell.
Just replace your existing button setup with the code above, and you'll have fully functional Move Up/Move Down buttons that preserve the checked state of your users—critical for keeping track of who's marked as a response group admin.
内容的提问来源于stack exchange,提问作者Graham J

