Checkboxlist selected items

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 6 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
mar10c--
Posts: 59
Last visit: Mon May 22, 2023 6:53 am

Checkboxlist selected items

Post by mar10c-- »

I can't seem to get this to work?

if($CheckBoxList1.SelectedItem -eq "earth"){
..run code here
}
elseif($CheckBoxList1.SelectedItem -eq "wind"){
..run code here
}
elseif($CheckBoxList1.SelectedItem -eq "fire"){
..run code here
}


Am i missing something?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checkboxlist selected items

Post by jvierra »

You didn't say what your problem is.
User avatar
mar10c--
Posts: 59
Last visit: Mon May 22, 2023 6:53 am

Re: Checkboxlist selected items

Post by mar10c-- »

Sorry I me meant to do something like this. What should the $items. be?

foreach ($items in $checkedlistbox1.SelectedItems)
{
if($items.('earth'))
{
Invoke-StoredProc1
}

elseif($items.('earth'))
{
Invoke-StoredProc2
}

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

Re: Checkboxlist selected items

Post by jvierra »

I have no idea what you put into the listbox. It could be full of anything.

Start here: http://www.sapien.com/blog/topics/spotl ... -controls/
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checkboxlist selected items

Post by jvierra »

If a box is loaded with strings like this:
PowerShell Code
Double-click the code block to select all.
$FormEvent_Load={
	$checkedlistbox1.Items.AddRange(@('earth','sky','moon'))`

}




$buttonGetItems_Click={
    foreach($item in $checkedlistbox1.CheckedItems){
        switch($item) {
            earth   { [void][System.Windows.Forms.MessageBox]::Show('Earth is home to gnats'    )  }
            sky     { [void][System.Windows.Forms.MessageBox]::Show('The sky is the limit'      )  }
            moon    { [void][System.Windows.Forms.MessageBox]::Show('Moonbeams are seldom noisy')  }
		}
	}
}
User avatar
mar10c--
Posts: 59
Last visit: Mon May 22, 2023 6:53 am

Re: Checkboxlist selected items

Post by mar10c-- »

the switch statement executes the moon only. I was looking to:
1. find all the checked ones in the checkboxlist
2. find the values of the checked ones then execute based on what the value is.

Thanks for your help.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checkboxlist selected items

Post by jvierra »

The switch statement is in a loop and executes for all checked items. I have used this code for years. Are you sure you know how to create the listbox?

Run the attached code to see what I mean.
Attachments
Demo-CheckedListbox.pff
(5.28 KiB) Downloaded 405 times
User avatar
mar10c--
Posts: 59
Last visit: Mon May 22, 2023 6:53 am

Re: Checkboxlist selected items

Post by mar10c-- »

My bad. It actually works. I had .selectedItems instead of .checkeditems

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

Re: Checkboxlist selected items

Post by jvierra »

Good.

Once you see how controls are equipped things are pretty easy. It is the initial learning that can be very confusing. PowerShell Studio makes all of it much easier.
This topic is 9 years and 6 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