Using Double Click for Adding and Removing Items from Lists

Double click functionality for movement of items between listboxes is commonly used within Windows, and quite intuitive. This article shows how we can implement this in our own forms based projects. We’ll use this example with ListView, but it is also applicable to ListBoxes (ListView controls inherit from ListBoxes).

For initial setup of this project, follow the steps below, using your preferred GUI editor or code.

  • Create a new forms project
  • Change the form’s control name and text if desired (called AddServers and Double Click Demo in this article)
  • Add two ListView controls, with names of lstFrom, and lstTo
  • Add two Button controls, with names of btnAdd, and btnRemove
  • (optional) Set the Text property of the form

lstFrom

  • Add a column to the form
    • Name : columnComputernameFrom
    • Sorting : Ascending
    • Text : Computername
    • View : Details
    • Add the following items to lstFrom , with Text values of Computer1, Computer2, Computer3, and Computer4

lstTo

  • Add a column to the form
    • Name : columnComputernameFrom
    • Text : Computername
    • View : Details
    • Sorting : Ascending

Buttons
Set the name of the top button to btnAdd, and the Text property to >
Set the name of the bottom button to btnRemove, and the Text property to <

Arrange the controls to look something like the picture below :

Our form design

Our form design

Once this is complete, we’re going to start adding some features.

Add/Remove Buttons

This could be described as the ‘classic’ method of adding and removing items from lists, with arrows pointing in alternative directions to indicate the addition or removal of an item from one listbox to another.

In the Load event, we’ll set the arrow buttons Enabled property to $false, since no items are selected.

Let’s activate or deactive the add and remove buttons depending on whether the appropriate listboxes are populated, and additionally disable the appropriate button when an item is selected. There’s no point of the add button being enabled when we have selected an item from the To listgridview for example.

Now we add the code for the button events, adding to removing from the appropriate listgridview. We use the FindItemWithText method to verify that the item does not already exist there, preventing duplications.

Now go ahead and run it. Select double click on an item on the left hand column, and it will add it to the right one, and versa.

Double Click - Running 1

Double click on one of the items

Double Click - Running 2

…..and it is moved to the other side

 

The mechanisms that double click provides for list also be used on other controls. For example, double clicking on an item in a list which automatically populates a textbox with the value that has been selected in the list.

You can find a copy of the PowerShell Studio .psf form file, and also an exported .ps1 of the same for at my repository on GitHub.

A video of this is also available on the PowerShell.Amsterdam YouTube channel.

Thanks for reading, and please provide feedback if you have comments, questions, or have noticed any errors in this article.

cheers,

Tim

One thought on “Using Double Click for Adding and Removing Items from Lists

  1. Pingback: Removing Items from Lists Using the Keyboard | PowerShell.Amsterdam

Leave a Reply

Your email address will not be published. Required fields are marked *