Page 1 of 1

function Load-ToolStripComboBox

Posted: Fri Jan 29, 2016 1:27 pm
by Hopkins2007
Product, version and build: 4.2.99
32 or 64 bit version of product: 64
Operating system: Windows 7
32 or 64 bit OS: 64

I added a MenuStrip to a form project and added several ComboBoxes to it. The first few have hard coded collections in the drop downs, but the last I fill depending on what is selected from the first two. I ran into a problem with the Load-ComboBox function you guys generate when we have a ComboBox in our forms because the -ComboBox parameter is looking for a ComboBox object and the object on the MenuStrip is of type ToolStripComboBox. You may want to add functionality so when a ToolStripComboBox is added to a form, this function is generated with it (note, I removed the DisplayMember parameter, a ToolStripComboBox objects don't have the method).
function Load-ToolStripComboBox {
<#
    .SYNOPSIS
        This functions helps you load items into a ToolStripComboBox.

    .DESCRIPTION
        Use this function to dynamically load items into the ToolStripComboBox control.

    .PARAMETER  ToolStripComboBox
        The ToolStripComboBox control you want to add items to.

    .PARAMETER  Items
        The object or objects you wish to load into the ToolStripComboBox's Items collection.
    
    .PARAMETER  Append
        Adds the item(s) to the ToolStripComboBox without clearing the Items collection.
    
    .EXAMPLE
        Load-ToolStripComboBox $ToolStripComboBox1 "Red", "White", "Blue"
    
    .EXAMPLE
        Load-ToolStripComboBox $ToolStripComboBox1 "Red" -Append
        Load-ToolStripComboBox $ToolStripComboBox1 "White" -Append
        Load-ToolStripComboBox $ToolStripComboBox1 "Blue" -Append
    
    .EXAMPLE
        Load-ToolStripComboBox $ToolStripComboBox1 (Get-Process) "ProcessName"
#>
	Param (
		[ValidateNotNull()]
		[Parameter(Mandatory = $true)]
		[System.Windows.Forms.ToolStripComboBox]$ToolStripComboBox,
		[ValidateNotNull()]
		[Parameter(Mandatory = $true)]
		$Items,
		[Parameter(Mandatory = $false)]
		[string]$DisplayMember,
		[switch]$Append
	)
	
	if (-not $Append) {
		$ToolStripComboBox.Items.Clear()
	}
	
	if ($Items -is [Object[]]) {
		$ToolStripComboBox.Items.AddRange($Items)
	} elseif ($Items -is [Array]) {
		$ToolStripComboBox.BeginUpdate()
		foreach ($obj in $Items) {
			$ToolStripComboBox.Items.Add($obj)
		}
		$ToolStripComboBox.EndUpdate()
	} else {
		$ToolStripComboBox.Items.Add($Items)
	}
}

Re: function Load-ToolStripComboBox

Posted: Fri Jan 29, 2016 1:28 pm
by Hopkins2007
Another fix is to remove the object requirement from the Load-ComboBox function you already have, but I think its better to verify your objects.

Re: function Load-ToolStripComboBox

Posted: Fri Jan 29, 2016 3:38 pm
by DevinL
Thanks for bringing this to our attention, I'll be sure to pass this on to the dev team.