deleting items from a listbox

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.
This topic is 9 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

Re: deleting items from a listbox

Post by boyddt_co »

one listbox with 6 items. highlight items 2, 4 & 6. click button and have items 2, 4 & 6 removed from the listbox. one click.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: deleting items from a listbox

Post by jvierra »

boyddt_co wrote:one listbox with 6 items. highlight items 2, 4 & 6. click button and have items 2, 4 & 6 removed from the listbox. one click.
Did you try the demo form I just posted? It does exactly that.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: deleting items from a listbox

Post by jvierra »

Here is a more complete version that shows how to move items in both directions with clicks or with buttons. It can move one, ten or all items on a click.
Attachments
Demo-ListBoxCapabilities.pff
(15.92 KiB) Downloaded 228 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: deleting items from a listbox

Post by jvierra »

This demo shows how to use relational list techniques with a listbox.
Attachments
Demo-ListBoxRelationalCascade.pff
(7.63 KiB) Downloaded 217 times
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

Re: deleting items from a listbox

Post by boyddt_co »

here is the code that does what I want. Thank you for your help.

$buttonClearSelectedSun_Click={

$selectedCount = $lb_sun.SelectedItems.Count
for($selectedCount;$selectedCount -ge 1; $selectedCount--)
{
$idx = $lb_sun.SelectedIndex
$lb_sun.Items.RemoveAt($idx)
}

}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: deleting items from a listbox

Post by jvierra »

Sorry but that code will not work as you think it will. It is basically doing nothing in a loop.

You loop does many executions of the same item.

$idx=$lb_sun.SelectedIndex
$lb_sun.Items.RemoveAt($idx)

This should do what you want:

while($idx=$lb_sun.SelectedIndex){
$lb_sun.Items.RemoveAt($idx)
}


In your is index is null you will blow up.
This topic is 9 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked