Chris Moretti
Chris Moretti

Reputation: 607

Powershell Checkbox that selects other checkboxes

I have a basic form UI in powershell. I have a series of 3 individual checkboxes with the following code:

    $CBlabel = New-Object System.Windows.Forms.Label
    $CBlabel .Location = New-Object System.Drawing.Point(10,125)
    $CBlabel .Size = New-Object System.Drawing.Size(280,20)
    $CBlabel .Text = "Select a box:"
    $form.Controls.Add($CBlabel )

    $cb1Checkbox = New-Object System.Windows.Forms.Checkbox
    $cb1Checkbox .Location = New-Object System.Drawing.Size(10,145)
    $cb1Checkbox .Size = New-Object System.Drawing.Size(280,20)
    $cb1Checkbox .Text = "Checkbox 1"
    $form.Controls.Add($cb1Checkbox )

    $cb2Checkbox = New-Object System.Windows.Forms.Checkbox
    $cb2Checkbox .Location = New-Object System.Drawing.Size(10,165)
    $cb2Checkbox .Size = New-Object System.Drawing.Size(280,20)
    $cb2Checkbox .Text = "Checkbox 2"
    $form.Controls.Add($cb2Checkbox )

    $cb3Checkbox = New-Object System.Windows.Forms.Checkbox
    $cb3Checkbox .Location = New-Object System.Drawing.Size(10,185)
    $cb3Checkbox .Size = New-Object System.Drawing.Size(280,20)
    $cb3Checkbox .Text = "Checkbox 3"
    $form.Controls.Add($cb3Checkbox )

I would like to add a 4th checkbox below this that says "Select All" and have it be dynamic if possible. If I select it, it automatically selects checkboxes 1, 2, and 3. If I unselect checkbox 2 (or any other checkbox), it automatically unselects the "Select All" checkbox. Is this possible?

Upvotes: 1

Views: 5341

Answers (2)

Mike321
Mike321

Reputation: 21

I have been playing around trying to achieve a more dynamic approach and came up with the following. Create a GroupBox ("$grpbox" the name in my example) and add it to the form, add the checkboxes to the group box instead of the form:

Add Checkboxes to groupbox (not form)

$grpBox.Controls.Add($cb1Checkbox)

call newly created function

$cb4Checkbox.Add_Click({clkSelectAll})

add new function to find all checkboxes within the groupboxes and check them

function clkSelectALL() {

    Foreach ($control in $grpbox.Controls){
        $objectType = $control.GetType().Name
        If ($objectType -like "CheckBox"){
            $control.checked = $true
        }
    }
}

And of course more logic could be added within the function.

Upvotes: 2

cet51
cet51

Reputation: 1226

Maybe this is a possible solution?

$cb4Checkbox = New-Object System.Windows.Forms.Checkbox
$cb4Checkbox.Location = New-Object System.Drawing.Size(x,y)
$cb4Checkbox.Size = New-Object System.Drawing.Size(x,y)
$cb4Checkbox.Text = "Checkbox 4 - Select All"
$cb4Checkbox.Add_Click({
    If ($cb4CheckBox.Checked -eq $true){
        $cb1Checkbox.Checked = $true
        $cb2Checkbox.Checked = $true
        $cb3Checkbox.Checked = $true
    }
})
$form.Controls.Add($cb4Checkbox)

For your request of unchecking the 'select all' checkbox, you would need to do a .add_click for all of your first three checkboxes that would cause the 'select all' checkbox to become unchecked. I believe a better way to do this would be to write a function that you could reference when any of the first three checkboxes are clicked on, and have it uncheck the 'select all' checkbox (see below)

function Uncheck-SelectAllBox
{
    $cb4Checkbox.Checked = $false
}

$CBlabel = New-Object System.Windows.Forms.Label
$CBlabel.Location = New-Object System.Drawing.Point(10,125)
$CBlabel.Size = New-Object System.Drawing.Size(280,20)
$CBlabel.Text = "Select a box:"
$form.Controls.Add($CBlabel)

$cb1Checkbox = New-Object System.Windows.Forms.Checkbox
$cb1Checkbox .Location = New-Object System.Drawing.Size(10,145)
$cb1Checkbox .Size = New-Object System.Drawing.Size(280,20)
$cb1Checkbox .Text = "Checkbox 1"
$cb1Checkbox.add_click({ Uncheck-SelectAllBox })
$form.Controls.Add($cb1Checkbox)

$cb2Checkbox = New-Object System.Windows.Forms.Checkbox
$cb2Checkbox .Location = New-Object System.Drawing.Size(10,165)
$cb2Checkbox .Size = New-Object System.Drawing.Size(280,20)
$cb2Checkbox .Text = "Checkbox 2"
$cb2Checkbox.add_click({ Uncheck-SelectAllBox })
$form.Controls.Add($cb2Checkbox)

$cb3Checkbox = New-Object System.Windows.Forms.Checkbox
$cb3Checkbox .Location = New-Object System.Drawing.Size(10,185)
$cb3Checkbox .Size = New-Object System.Drawing.Size(280,20)
$cb3Checkbox .Text = "Checkbox 3"
$cb3Checkbox.add_click({ Uncheck-SelectAllBox })
$form.Controls.Add($cb3Checkbox)

$cb4Checkbox = New-Object System.Windows.Forms.Checkbox
$cb4Checkbox.Location = New-Object System.Drawing.Size(x,y)
$cb4Checkbox.Size = New-Object System.Drawing.Size(x,y)
$cb4Checkbox.Text = "Checkbox 4 - Select All"
$cb4Checkbox.Add_Click({
    If ($cb4CheckBox.Checked -eq $true){
        $cb1Checkbox.Checked = $true
        $cb2Checkbox.Checked = $true
        $cb3Checkbox.Checked = $true
    }
})
$form.Controls.Add($cb4Checkbox)

Upvotes: 3

Related Questions