How to check state of checkboxes?

Jan 14, 2013 at 7:13 PM
Edited Jan 14, 2013 at 7:15 PM

Hi all,

I'm very new to powershell and have little experience with C#. I have created a GUI with checkboxes, and I want certain functions to be executed if those check boxes are checked. Here is my code (all inside a StackPanel):

        New-Label "Office 365 Migration Tool" -FontFamily "Helvetica" -FontSize 20
        New-Label "I would like to migrate the following`nfrom Exchange 2010 to Office 365:" `
               -FontFamily "Helvetica" `
               -FontSize 12 

        New-Separator
        New-CheckBox "Mailbox Properties" -FontFamily "Helvetica" -FontSize 12
        New-Label "Description" -FontFamily "Helvetica" -FontSize 10
        
        New-Separator
        New-CheckBox "Mailbox Permissions" -FontFamily "Helvetica" -FontSize 12
        New-Label "Description" -FontFamily "Helvetica" -FontSize 10
        
        New-Separator
        New-CheckBox "Contacts" -FontFamily "Helvetica" -FontSize 12 
        New-Label "Description" -FontFamily "Helvetica" -FontSize 10

        New-Separator
        New-CheckBox "Contact Permissions"  -FontFamily "Helvetica" -FontSize 12
        New-Label "Description" -FontFamily "Helvetica" -FontSize 10
        
        New-Separator
        New-CheckBox "Distribution Groups" -FontFamily "Helvetica" -FontSize 12
        New-Label "Description" -FontFamily "Helvetica" -FontSize 10
         
        New-Separator
        New-CheckBox "Calendar Permissions" -FontFamily "Helvetica" -FontSize 12
        New-Label "Description" -FontFamily "Helvetica" -FontSize 10

        New-Separator
        New-Label "This program must be run from a server with `
administrative access to Microsoft Exchange. `
When you are ready to proceed, click OK." `
                   -FontFamily "Helvetica" -FontSize 12
        
        New-Button "OK" -FontSize 15 -Margin 5 -On_Click {    
            if(<# checkbox A is checked #>) { <# execute function A #> }
        
        }

I tried playing around with -isChecked, but I couldn't figure out how to reference each checkbox. If I add a variable name in front of each new checkbox, the checkbox disappears from the window. Adding the -show argument makes each checkbox show up one at a time in a separate window.

Any thoughts on how to implement the commented sections at the end?

Coordinator
Jan 15, 2013 at 6:08 AM

Well, the simplest thing to do is collect the values when they hit OK and then handle the logic after the UI closes ...

# This is just a shortcut to setting these properties over and over
Set-UIStyle "L1" @{ FontFamily = "Helvetica"; FontSize = 10; TextWrapping = "WrapWithOverflow" }
Set-UIStyle "C1" @{ FontFamily = "Helvetica"; FontSize = 12 }

# Now show the UI and collect the results:
$Options = New-StackPanel -Margin 5 {

    New-TextBlock "Office 365 Migration Tool" -FontFamily Helvetica -FontSize 20
    New-TextBlock "I would like to migrate the following from Exchange 2010 to Office 365:" -VisualStyle C1 -TextWrapping "WrapWithOverflow"

    New-UniformGrid -Columns 2 -Margin "5,10" {

        New-CheckBox "Mailbox Properties`n" -Name "MailboxProperties" -VisualStyle C1
        New-TextBlock "This is a really long description of the Mailbox Properties that will be migrated if you check that box..."  -VisualStyle L1
        
        New-CheckBox "Mailbox Permissions" -Name "MailboxPermissions" -VisualStyle C1
        New-TextBlock "This is a Description of the Mailbox Permissions"  -VisualStyle L1
        
        New-CheckBox "Contacts" -Name "Contacts" -VisualStyle C1
        New-TextBlock "This is a Description of the Contacts" -VisualStyle L1

        New-CheckBox "Contact Permissions" -Name "ContactPermissions" -VisualStyle C1
        New-TextBlock "This is a Description of the Contact Permissions"  -VisualStyle L1
        
        New-CheckBox "Distribution Groups" -Name "DistributionGroups" -VisualStyle C1
        New-TextBlock "This is a Description of the Distribution Groups"  -VisualStyle L1
        
        New-CheckBox "Calendar Permissions" -Name "CalendarPermissions" -VisualStyle C1
        New-TextBlock "This is a Description of the Calendar Permissions" -VisualStyle L1
    }

    New-TextBlock "This program must be run from a server with administrative access to Microsoft Exchange. When you are ready to proceed, click OK." -VisualStyle L1
        
    New-Button "OK" -FontSize 15 -Margin 5 -Width 120 -On_Click {    
        Get-ParentControl | Set-UIValue -passThru | Close-Control  
    }

} -Width 500 -Show

At that point you've got a variable $Options which is a hash table of the checkbox names to booleans indicating whether they were checked or not, so you can do your logic very simply like:

if($Options.MailboxProperties) { 
   # Code here for Mailbox Properties
}
if($Options.MailboxPermissions) {
   # Code here for Mailbox Permissions
}

... etc.

Bottom line: give your control a -Name, and then use Set-UIValue ... and when you close the window, the output will flow.

Jan 15, 2013 at 2:10 PM

Perfect, that's exactly what I was looking for, and thank you for the code clean up!

Jun 13, 2013 at 6:24 PM
Hi,

Sorry for hijacking this thread but it was the closest to what I was looking for. I'm trying something similar but with buttons as opposed to check boxes. I give each of my buttons a name but nothing is returned. Is there there some way to get this to work with buttons?

Thank you,

Mike