function Load-ToolStripComboBox

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
Hopkins2007
Posts: 5
Last visit: Tue May 24, 2022 9:19 am

function Load-ToolStripComboBox

Post 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)
	}
}
User avatar
Hopkins2007
Posts: 5
Last visit: Tue May 24, 2022 9:19 am

Re: function Load-ToolStripComboBox

Post 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.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: function Load-ToolStripComboBox

Post by DevinL »

Thanks for bringing this to our attention, I'll be sure to pass this on to the dev team.
DevinL
SAPIEN Technologies, Inc.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.