Reorder list box by drag and drop

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
timstspry17
Posts: 40
Joined: Sun Sep 06, 2015 9:31 pm

Reorder list box by drag and drop

Post by timstspry17 » Tue Sep 17, 2019 3:47 am

Hello, I am looking for help with writing a listbox control that will initially be populated with a handful of servernames. The servernames do not show up in any particular order, done on purpose, because I want to be able to rearrange the items in the list box by simply dragging them within the list box to reorder them.

Any help would be appreciated!

Tim

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Reorder list box by drag and drop

Post by jvierra » Tue Sep 17, 2019 11:02 am

You will have to enable drag/drop for the ListBox and write the code to manage the drag/drop events.

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Reorder list box by drag and drop

Post by jvierra » Tue Sep 17, 2019 11:04 am

Here is a short tutorial on how to use drag/drop with listboxes:
https://www.codeproject.com/Articles/20 ... r-s-Tutori

User avatar
jvierra
Posts: 13793
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Reorder list box by drag and drop

Post by jvierra » Tue Sep 17, 2019 2:41 pm

I should also note that there is no way to use drag/drop to reorder items in a single ListBox. You would have to write custom code to select and move an item in the list order.

You can use the "MouseDown" event to select the item but getting the target will require some difficult coding that can display an insertion point while hovering. This will likely require adding some C# code helpers to the form.

User avatar
timstspry17
Posts: 40
Joined: Sun Sep 06, 2015 9:31 pm

Re: Reorder list box by drag and drop

Post by timstspry17 » Wed Sep 18, 2019 4:12 am

Thanks for the information. Looks a bit more involved than I had hoped. Will probably use 2 list boxes as I have seen examples of. Doing it in a single list box would have been nice, but 2 list boxes will work too.

Tim

Post Reply