none
Combobox Powershell stays empty(!) RRS feed

  • Question

  • Hi,

    I have this combobox which I try to load with example data but it says empty!

    Please advise.
    J.

    $MainForm_Load={
    #TODO: Initialize Form Controls here
    
    }
    
    
    
    
    
    #region Control Helper Functions
    function Load-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
    		Load-ListBox $ListBox1 "Red", "White", "Blue"
    	
    	.EXAMPLE
    		Load-ListBox $listBox1 "Red" -Append
    		Load-ListBox $listBox1 "White" -Append
    		Load-ListBox $listBox1 "Blue" -Append
    	
    	.EXAMPLE
    		Load-ListBox $listBox1 (Get-Process) "ProcessName"
    #>
    	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	
    }
    
    function Load-ComboBox 
    {
    <#
    	.SYNOPSIS
    		This functions helps you load items into a ComboBox.
    
    	.DESCRIPTION
    		Use this function to dynamically load items into the ComboBox control.
    
    	.PARAMETER  ComboBox
    		The ComboBox control you want to add items to.
    
    	.PARAMETER  Items
    		The object or objects you wish to load into the ComboBox's Items collection.
    
    	.PARAMETER  DisplayMember
    		Indicates the property to display for the items in this control.
    	
    	.PARAMETER  Append
    		Adds the item(s) to the ComboBox without clearing the Items collection.
    	
    	.EXAMPLE
    		Load-ComboBox $combobox1 "Red", "White", "Blue"
    	
    	.EXAMPLE
    		Load-ComboBox $combobox1 "Red" -Append
    		Load-ComboBox $combobox1 "White" -Append
    		Load-ComboBox $combobox1 "Blue" -Append
    	
    	.EXAMPLE
    		Load-ComboBox $combobox1 (Get-Process) "ProcessName"
    #>
    	Param (
    		[ValidateNotNull()]
    		[Parameter(Mandatory=$true)]
    		[System.Windows.Forms.ComboBox]$ComboBox,
    		[ValidateNotNull()]
    		[Parameter(Mandatory=$true)]
    		$Items,
    	    [Parameter(Mandatory=$false)]
    		[string]$DisplayMember,
    		[switch]$Append
    	)
    	
    	if(-not $Append)
    	{
    		$ComboBox.Items.Clear()	
    	}
    	
    	if($Items -is [Object[]])
    	{
    		$ComboBox.Items.AddRange($Items)
    	}
    	elseif ($Items -is [Array])
    	{
    		$ComboBox.BeginUpdate()
    		foreach($obj in $Items)
    		{
    			$ComboBox.Items.Add($obj)	
    		}
    		$ComboBox.EndUpdate()
    	}
    	else
    	{
    		$ComboBox.Items.Add($Items)	
    	}
    
    	$ComboBox.DisplayMember = $DisplayMember	
    }
    #endregion
    
    $combobox1_SelectedIndexChanged={
    	#TODO: Place custom script here
    	#Load-ComboBox $combobox1 (Get-Process) "ProcessName"
    	Load-ComboBox $combobox1 "Red" -Append
    	Load-ComboBox $combobox1 "White" -Append
    	Load-ComboBox $combobox1 "Blue" -Append
    }
    


    Jan Hoedt

    Monday, September 14, 2015 5:03 PM

Answers

  • Load-ComboBox $combobox1 "Red" -Append
    Load-ComboBox $combobox1 "White" -Append
    Load-ComboBox $combobox1 "Blue" -Append

    should be in the $MainForm_Load={
    #TODO: Initialize Form Controls here

    }

    not in the $combobox1


    Jan Hoedt

    • Marked as answer by janhoedt Tuesday, September 15, 2015 9:30 AM
    Tuesday, September 15, 2015 9:30 AM

All replies

  • Don't use the function.  It requires an intimate understanding of objects. The following works as expected.

    $combobox1.DataSource=[collections.arraylist](Get-Process)
    $combobox1.DisplayMember='Name'


    \_(ツ)_/

    Monday, September 14, 2015 5:14 PM
  • Load-ComboBox $combobox1 "Red" -Append
    Load-ComboBox $combobox1 "White" -Append
    Load-ComboBox $combobox1 "Blue" -Append

    should be in the $MainForm_Load={
    #TODO: Initialize Form Controls here

    }

    not in the $combobox1


    Jan Hoedt

    • Marked as answer by janhoedt Tuesday, September 15, 2015 9:30 AM
    Tuesday, September 15, 2015 9:30 AM
  • Yes that is what all of the forums have been trying to tell you.

    \_(ツ)_/

    Tuesday, September 15, 2015 9:34 AM