Getting the integer for your checkedlistbox

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 10 years and 5 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
barrybloom
Posts: 36
Last visit: Mon Mar 11, 2024 2:23 am

Getting the integer for your checkedlistbox

Post by barrybloom »

Hi All,

Found out how to uncheck my checkboxes but for the life of me I'm not sure how to reflect the integer for the check box I wish to uncheck.

The code below is nested in another ForEach that resolves to make the match with the $existingGroup to the $item entry in the checkedlistboxes.

On that match I need to uncheck that specific box, but alas it does nothing.

Any advice appreciated.

Barry
PowerShell Code
Double-click the code block to select all.
foreach ($item in $checkedlistbox1.CheckedItems){
			$i = 0
			If ($item -eq $existingGroup){
				$checkedListBox1.SetItemCheckState($i,'Unchecked')
			}
			$i = $i+1
		}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting the integer for your checkedlistbox

Post by jvierra »

What are you trying to match with? $item is a checked list object an is not a string or group item.
User avatar
barrybloom
Posts: 36
Last visit: Mon Mar 11, 2024 2:23 am

Re: Getting the integer for your checkedlistbox

Post by barrybloom »

Hi,

OK I am parsing a list of AD groups the user is a member of stored in an array $existingGroup.

In the foreach loop $item in $checkedlistbox1.CheckedItems, if $item matches the $group in the array $existingGroup (they are named exactly the same) I then wish to uncheck the corresponding check box with the same name as the AD group.

I have echoed out if there is a match so I know it is finding the group / checkbox that are the same. I cant appear to get the mechanism to remove the check.

That make better sense?

Thanks,

Barry
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Getting the integer for your checkedlistbox

Post by davidc »

FYI I moved this post to the GUIs forum.

Your script incorrectly assumes the CheckItems positional index is the same as the index of the item in the CheckedListBox.

I recommend using the CheckedIndices property to access the items instead.
PowerShell Code
Double-click the code block to select all.
foreach($index in $checkedlistbox1.CheckedIndices)
{
	$item = $checkedlistbox1.Items[$index]
	...
}
David
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting the integer for your checkedlistbox

Post by jvierra »

barryb wrote:Hi,

OK I am parsing a list of AD groups the user is a member of stored in an array $existingGroup.

In the foreach loop $item in $checkedlistbox1.CheckedItems, if $item matches the $group in the array $existingGroup (they are named exactly the same) I then wish to uncheck the corresponding check box with the same name as the AD group.

I have echoed out if there is a match so I know it is finding the group / checkbox that are the same. I cant appear to get the mechanism to remove the check.

That make better sense?

Thanks,

Barry
How do you know which element of the array is the one you want to match. You can only match one to one. What is the criteria? What is it that you are trying to accomplish?

The checked boxes will check and uncheck automatically if set to do so. If you want to remove a check then use David's suggestion of enumerating the indices and then match the item
PowerShell Code
Double-click the code block to select all.
$checkedlistbox1.SetItemCheckState($index,[System.Windows.Forms.CheckState]::Unchecked)
This topic is 10 years and 5 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