Search code examples
powershellcheckboxlist

Clearing checkboxes in a checkedboxlist in powershell studio


I have a giant checkedboxlist that I have created that I need a button to clear all of the checked items with. Here is what I have:

 $btnResetGroups_Click = {
    foreach ($chkbox in $chklistGroups)
    {
        $chkbox.ClearSelected() # I tried to do $chkbox.Checked = $false but it doesn't recognize 'checked' as a property
    }

    #   $chklistGroups.ClearSelected()
}

Is there a reason why this doesn't work?

EDIT: This is the code that populates the checkboxlist:

$formPTINewUserCreation_Load={
    Import-Module ActiveDirectory

    # read in the XML files
    [xml]$NewUserXML = Get-Content -LiteralPath \\papertransport.com\files\UserDocuments\mneis\Code\XML\NewUserTest.xml

    # popoulate the checklist with the groups from active directory
    $AD_ResourceGroups = Get-ADGroup -filter * -SearchBase "OU=Resource Groups,OU=Groups,OU=Paper Transport,DC=papertransport,DC=com"
    $AD_ResourceGroups | ForEach-Object { $chklistGroups.Items.Add($_.name) }
}

This is the GUI itself:


Solution

  • To ensure that every checkbox is unchecked when the button is clicked the code must loop through the container holding the Checkboxes.

    $btnResetGroups_Click = {
        # get the container (in this case it looks like a list box)
        $myCheckBoxes = $myListBox.Items
        # next loop through each checkbox in the array of checkbox objects
        foreach ($chkbox in $myCheckBoxes)
        {
            $chkbox.Checked = $false
        }
    }
    

    This will ensure that every checkbox in the container is set to false. From the style of the GUI I'm assuming this is WPF and not Winforms.