Alan's Blog

"Yeah. I wrote a script that will do that."

Menu
  • About My Blog
Menu

Move Selected Item Up or Down in PowerShell Listbox

Posted on May 30, 2016 by Alan

Listbox Move Up Down Buttons.ps1 shows the event handlers for moving a selected item up and down in a PowerShell listbox. I wrote it because couldn’t find it anywhere else.

$handler_DownButton_Click= 
{
#only if the last item isn't the current one
   if(($ListBox1.SelectedIndex -ne -1)   -and   ($ListBox1.SelectedIndex -lt $ListBox1.Items.Count - 1)    )   {
        $listbox1.BeginUpdate()
        #Get starting position 
        $pos = $Listbox1.selectedIndex
        # add a duplicate of item below in the listbox
        $ListBox1.items.insert($pos,$listbox1.Items.Item($pos +1))
        # delete the old occurrence of this item
        $ListBox1.Items.RemoveAt($pos +2 )
        # move to current item
        $ListBox1.SelectedIndex = ($pos +1)
        $ListBox1.EndUpdate()
   }ELSE{
       #Bottom of list, beep
       [console]::beep(500,100)
   }
}

$handler_UpButton_Click= 
{
 # only if the first item isn't the current one
    if($ListBox1.SelectedIndex -gt 0)
    {
        $listbox1.BeginUpdate()
        #Get starting position
        $pos = $Listbox1.selectedIndex
        # add a duplicate of original item up in the listbox
        $ListBox1.items.insert($pos -1,$listbox1.Items.Item($pos))
        # make it the current item
        $ListBox1.SelectedIndex = ($pos -1)
        # delete the old occurrence of this item
        $ListBox1.Items.RemoveAt($pos +1)
        $ListBox1.EndUpdate()
    }ELSE{
       #Top of list, beep
       [console]::beep(500,100)
   }
}

Some things to note. I have error handling for moving too far up or down. The error is a beep using the [console] accelerator. BeginUpdate turns off the display while making changes to the listbox. EndUpdate turns them back on.

Script Text

Leave a Reply

You must be logged in to post a comment.

Search

Please Note

All the scripts are saved as .txt files. Newer files have a “View Script” button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use “Save Target As”. Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with version 3 or later.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient.  If you find a post error or a script which doesn’t work as expected, I appreciate being notified.  My email is my first name at the domain name, and you are welcome to contact me that way.

Tags

1E ACLS Active Directory ActiveDirectory ADSI Advanced Functions Audit Change Administrator Password COMObject Computer Groups DateTime Desktop DNS Excel FileScriptingObject Forms General GPO GPS Group Policy Hacks ISE Lockout logons NAV740 Nessus OU OU permissions Outlook Pick Folder Power PowerShell Powershell Scriptlets RDP SCCM schedule reboot Scripting Security Shell.Application user information VBA Windows Update WMI WordPress WPF

Categories

akaplan.com

  • Back to Home Page

Archives

Scripting Sites

  • A Big Pile of Small Things
  • Adam, the Automator
  • Art of the DBA
  • Ashley McGlone
  • Boe Prox
  • Carlo Mancini
  • DexterPOSH
  • Doug Finke
  • Jaap Brasser's Blog
  • JeffOps The Scripting Dutchman
  • Jonathan Medd's Blog
  • Keith Hill's Blog
  • LazyWinAdmin
  • Nana Lakshmanan
  • PowerShell Magazine
  • PowerShell Team Blog
  • PowerShell.org
  • PwrShell.net
  • Richard Siddaway's Blog
  • Ryan Yates' Blog
  • Skatterbrainz
  • The Lonely Administrator

SQL Site

  • Art of the DBA

Meta

  • Register
  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
©2025 Alan's Blog | Theme by SuperbThemes

Terms and Conditions - Privacy Policy