Public/Remove-WFListBoxItem.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
function Remove-WFListBoxItem { <# .SYNOPSIS Function to remove item(s) from a ListBox Control .DESCRIPTION Function to remove item(s) from a ListBox Control .PARAMETER ListBox Specifies the ListBox control .PARAMETER All Specifies that you want to remove all the item .PARAMETER Pattern Specifies that you want to remove items with a specific pattern .PARAMETER SelectedItems Specifies that you want to remove the selected items .NOTES Author: Francois-Xavier Cat Twitter:@LazyWinAdm www.lazywinadmin.com github.com/lazywinadmin #> [CmdletBinding(DefaultParameterSetName = 'All', SupportsShouldProcess = $true)] param ( [Parameter(ParameterSetName = 'All', Mandatory = $true)] [Parameter(ParameterSetName = 'Pattern', Mandatory = $true)] [Parameter(ParameterSetName = 'Selected', Mandatory = $true)] [ValidateNotNull()] [System.Windows.Forms.ListBox]$ListBox, [Parameter(ParameterSetName = 'All', Mandatory = $true)] [Switch]$All, [Parameter(ParameterSetName = 'Pattern', Mandatory = $true)] [String[]]$Pattern, [Parameter(ParameterSetName = 'Selected', Mandatory = $true)] [Switch]$SelectedItems ) #Requires -Version 3 BEGIN { Add-Type -AssemblyName System.Windows.Forms Write-Verbose -Message "BEGIN - ListBox - Begining to update" $ListBox.BeginUpdate() } PROCESS { IF ($PSBoundParameters['All']) { Write-Verbose -Message "PROCESS - ListBox - Clear all item(s)" IF ($PSCmdlet.ShouldProcess($ListBox,"Clear all item(s)")) { $ListBox.Items.Clear() } } IF ($PSBoundParameters['Pattern']) { Write-Verbose -Message "PROCESS - ListBox - Clear item(s) with specific pattern" foreach ($item in $ListBox.Items) { foreach ($Text in $Pattern) { IF ($item -like $Text) { IF ($PSCmdlet.ShouldProcess($ListBox, "Remove Item with pattern $pattern : $item")) { Write-Verbose -Message "PROCESS - ListBox - Removing item: $item" $ListBox.Items.Remove($item) } } } } } IF ($PSBoundParameters['SelectedItems']) { while ($ListBox.SelectedItems -gt 0) { foreach ($item in $ListBox.SelectedItems) { IF ($PSCmdlet.ShouldProcess($ListBox, "Remove selected Item $item")) { Write-Verbose -Message "PROCESS - ListBox - Removing selected item: $item" $ListBox.Items.Remove($item) } } } } } END { Write-Verbose -Message "END - ListBox - End of update" $ListBox.EndUpdate() } } |