Public/Select/Select-ISETheme.ps1

function Select-ISETheme {

[cmdletbinding()]
Param()
    #========================================================================
    # Code Generated By: SAPIEN Technologies, Inc., PowerShell Studio 2014 v4.1.46
    # Generated On: 3/18/2014 10:19 AM
    # Generated By: Jeff Pollock
    #========================================================================
        <#
        .SYNOPSIS
            Selects and applies an ISE theme from the registry.
 
        .DESCRIPTION
            Selects and applies an ISE theme from the registry.
 
        .EXAMPLE
            PS C:\> Select-ISETheme
 
            Selects an ISE theme from the registry
 
        .NOTES
            Part of the ISEColorThemeCmdlets.ps1 Script by Jeff Pollock
            http://Lifeinpowerhsell.blogspot.com
            http://gallery.technet.microsoft.com/ISE-Color-Theme-Cmdlets-24905f9e
    #>

    #----------------------------------------------
    #region Application Functions
    #----------------------------------------------

    function OnApplicationLoad {
        return $true #return true for success or false for failure
    }

    function OnApplicationExit {
        $script:ExitCode = 0 #Set the exit code for the Packager
    }

    #endregion Application Functions

    #----------------------------------------------
    # Generated Form Function
    #----------------------------------------------
    function Start-ThemeSelector {

        #----------------------------------------------
        #region Import the Assemblies
        #----------------------------------------------
        [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
        [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
        [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
        [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
        [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
        [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
        [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
        [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
        [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
        #endregion Import Assemblies

        #----------------------------------------------
        #region Generated Form Objects
        #----------------------------------------------
        [System.Windows.Forms.Application]::EnableVisualStyles()
        $formISEThemeSelector = New-Object 'System.Windows.Forms.Form'
        $buttonExit = New-Object 'System.Windows.Forms.Button'
        $buttonSelectTheme = New-Object 'System.Windows.Forms.Button'
        $listboxThemes = New-Object 'System.Windows.Forms.ListBox'
        $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
        $formISEThemeSelector.Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path) 
        #endregion Generated Form Objects

        #----------------------------------------------
        # User Generated Script
        #----------------------------------------------
        $buttonSelectTheme_Click={
            Set-ISETheme -ThemeName $listboxThemes.SelectedItem
            $formISEThemeSelector.Close()
        }

        $formISEThemeSelector_KeyDown={
            if ($_.KeyCode -eq 'Enter') {Write-Output 'Inside';$buttonSelectTheme_Click}
        }

        $listboxThemes_KeyDown={
            if ($_.KeyCode -eq 'Enter') {
                Set-ISETheme -ThemeName $listboxThemes.SelectedItem
                $formISEThemeSelector.Close()
            }
        }
        
        $buttonExit_Click={$formISEThemeSelector.Close()}    
    
        $formISEThemeSelector_Load={
            #TODO: Initialize Form Controls here
            $Themes = Get-ImportedISETheme | Select-Object  ThemeName
            $Themes | ForEach-Object {
                Set-ListBox $listboxThemes $_.ThemeName -Append
            }        
        }
    
        #region Control Helper Functions
        function Set-ListBox 
        {
        <#
            .SYNOPSIS
                This functions helps you load items into a ListBox or CheckedListBox.
     
            .DESCRIPTION
                Use this function to dynamically load items into the ListBox control.
     
            .PARAMETER ListBox
                The ListBox control you want to add items to.
     
            .PARAMETER Items
                The object or objects you wish to load into the ListBox's Items collection.
     
            .PARAMETER DisplayMember
                Indicates the property to display for the items in this control.
         
            .PARAMETER Append
                Adds the item(s) to the ListBox without clearing the Items collection.
         
            .EXAMPLE
                Set-ListBox $ListBox1 "Red", "White", "Blue"
         
            .EXAMPLE
                Set-ListBox $listBox1 "Red" -Append
                Set-ListBox $listBox1 "White" -Append
                Set-ListBox $listBox1 "Blue" -Append
         
            .EXAMPLE
                Set-ListBox $listBox1 (Get-Process) "ProcessName"
        #>

        [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions','')]
        param (
                [ValidateNotNull()]
                [Parameter(Mandatory=$true)]
                [System.Windows.Forms.ListBox]$ListBox,
                [ValidateNotNull()]
                [Parameter(Mandatory=$true)]
                $Items,
                [Parameter(Mandatory=$false)]
                [string]$DisplayMember,
                [switch]$Append
            )
        
            if(-not $Append)
            {
                $listBox.Items.Clear()    
            }
        
            if($Items -is [System.Windows.Forms.ListBox+ObjectCollection])
            {
                $listBox.Items.AddRange($Items)
            }
            elseif ($Items -is [Array])
            {
                $listBox.BeginUpdate()
                foreach($obj in $Items)
                {
                    $listBox.Items.Add($obj)
                }
                $listBox.EndUpdate()
            }
            else
            {
                $listBox.Items.Add($Items)    
            }
    
            $listBox.DisplayMember = $DisplayMember    
        }
        #endregion
    
        # --End User Generated Script--
        #----------------------------------------------
        #region Generated Events
        #----------------------------------------------
    
        $Form_StateCorrection_Load=
        {
            #Correct the initial state of the form to prevent the .Net maximized form issue
            $formISEThemeSelector.WindowState = $InitialFormWindowState
        }
    
        $Form_Cleanup_FormClosed=
        {
            #Remove all event handlers from the controls
            try
            {
                $formISEThemeSelector.remove_Load($formISEThemeSelector_Load)
                $formISEThemeSelector.remove_Load($Form_StateCorrection_Load)
                $formISEThemeSelector.remove_FormClosed($Form_Cleanup_FormClosed)
            }
            catch [Exception]
            {$null }
        }
        #endregion Generated Events

        #----------------------------------------------
        #region Generated Form Code
        #----------------------------------------------
        $formISEThemeSelector.SuspendLayout()
        #
        # formISEThemeSelector
        #
        $formISEThemeSelector.Controls.Add($buttonExit)
        $formISEThemeSelector.Controls.Add($buttonSelectTheme)
        $formISEThemeSelector.Controls.Add($listboxThemes)
        $formISEThemeSelector.ClientSize = '228, 278'
        $formISEThemeSelector.FormBorderStyle = 'None'
        $formISEThemeSelector.Name = 'formISEThemeSelector'
        $formISEThemeSelector.Text = 'Theme Selector'
        $formISEThemeSelector.add_Load($formISEThemeSelector_Load)
        $formISEThemeSelector.StartPosition = 'CenterScreen'
        $formISEThemeSelector.MinimizeBox = $False
        $formISEThemeSelector.MaximizeBox = $False
        $formISEThemeSelector.BackColor =  $PopupColor
        $formISEThemeSelector.Add_KeyDown($formISEThemeSelector_KeyDown) 
        #
        # buttonExit
        #
        $buttonExit.Location = '117, 233'
        $buttonExit.Name = 'buttonExit'
        $buttonExit.Size = '96, 30'
        $buttonExit.TabIndex = 2
        $buttonExit.Text = 'E&xit'
        $buttonExit.UseVisualStyleBackColor = $True
        $buttonExit.add_Click($buttonExit_Click)
        #
        # buttonSelectTheme
        #
        $buttonSelectTheme.Location = '14, 233'
        $buttonSelectTheme.Name = 'buttonSelectTheme'
        $buttonSelectTheme.Size = '96, 30'
        $buttonSelectTheme.TabIndex = 1
        $buttonSelectTheme.Text = '&Select'
        $buttonSelectTheme.UseVisualStyleBackColor = $True
        $buttonSelectTheme.add_Click($buttonSelectTheme_Click)
        #
        # listboxThemes
        #
        $listboxThemes.Font = 'Microsoft Sans Serif, 12pt'        
        $listboxThemes.FormattingEnabled = $True
        $listboxThemes.ItemHeight = 17
        $listboxThemes.Location = '14, 15'
        $listboxThemes.Name = 'listboxThemes'
        $listboxThemes.Size = '199, 220'
        $listboxThemes.TabIndex = 0
        $listboxThemes.add_KeyDown($listboxThemes_KeyDown)


        $formISEThemeSelector.ResumeLayout($false)

        #endregion Generated Form Code

        #----------------------------------------------

        #Save the initial state of the form
        $InitialFormWindowState = $formISEThemeSelector.WindowState
        #Init the OnLoad event to correct the initial state of the form
        $formISEThemeSelector.add_Load($Form_StateCorrection_Load)
        #Clean up the control events
        $formISEThemeSelector.add_FormClosed($Form_Cleanup_FormClosed)
        #Show the Form
        return $formISEThemeSelector.ShowDialog()

    } #End Function

    #Call OnApplicationLoad to initialize
    if((OnApplicationLoad) -eq $true)
    {
        $BackgroundColor = $psISE.Options.ScriptPaneBackgroundColor
        IF ($BackgroundColor.A -lt 235) {$BackgroundColor.A = $BackgroundColor.A + 20}
        IF ($BackgroundColor.R -lt 235) {$BackgroundColor.R = $BackgroundColor.R + 20}
        IF ($BackgroundColor.G -lt 235) {$BackgroundColor.G = $BackgroundColor.G + 20}
        IF ($BackgroundColor.B -lt 235) {$BackgroundColor.B = $BackgroundColor.B + 20}
        $PopupColor = Convert-ARGBToHex "$($BackgroundColor.A),$($BackgroundColor.R),$($BackgroundColor.G),$($BackgroundColor.B)"
        #Call the form
        Start-ThemeSelector | Out-Null
        #Perform cleanup
        OnApplicationExit
    }
    

}