Automate, Accelerate, Accurate

Main menu

Category Archives: youtube

Drag and drop is a wonderful facility that most of us will use every day without even thinking about it. This article shows how it can be implemented in PowerShell GUI applications.

For this particular script, we’ll implement drag and drop functionality to allow us to drag a file from explorer to a textbox in we’ve created. When the drag and drop operation is complete, the textbox will then show the path to the file.

In an earlier post, we created a form with two listgridview controls, and used both add and remove buttons, and double click functionality to allow an item to move an item from one control to another in an intuitive manner.

Todays post shows how we can use a remove items from a list by pressing a key. Specifically, we’ll be using the DELETE key for removing items. This will be carried out in Sapien’s PowerShell Studio 2015, but the same methodology applies to however you generate winforms in PowerShell.

For initial setup, carry out the following :

create a new form with a header and several items.

create a listview control called $lstControl

populate the control with a header

add some items to the listview control

Sample form and control layout

select the control

select Events in the properties pane

double click on KeyDown

Insert the following code :

PowerShell

1

2

3

4

5

6

7

8

9

$lstControl_KeyDown=[System.Windows.Forms.KeyEventHandler]{

If($_.KeyCode-eq[System.Windows.Forms.Keys]::Delete)

{

ForEach($itemin$lstControl.SelectedItems)

{

$lstControl.Items.Remove($item)

}

}

}

The KeyDown event’s code

Go ahead and test it, adding an item to the control on the right hand side, selecting it, and then pressing the DELETE key.

Also, because the code loops through each selected item in the control, we can also use standard multiple selection functionality offered by the various use of the SHIFT and CTRL combinations.

Two items are selected using a click SHIFT combination

The DELETe key is pressed, and the previous selected items are removed from the list

You can find the files, if you are using PowerShell Studio 2015, or exported .ps1 file at my gitlab repository.

Additionally, there’s a (basic) video of this code in action at my channel on You Tube.