ComboBox - SelectedItem Displays Length Value

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 9 years and 8 months 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
seaeric
Posts: 5
Last visit: Tue Jul 08, 2014 11:15 am

ComboBox - SelectedItem Displays Length Value

Post by seaeric »

I created a simple GUI to learn more about the Combobox control. When I try to return the value of the selected item, I get output of the Length of the item vs. the string output. I've tried adding tostring() but it doesn't make any difference. Any help or know links discussing this topic would be appreciated.
PowerShell Code
Double-click the code block to select all.
function OnApplicationLoad {
	#Note: This function is not called in Projects
	#Note: This function runs before the form is created
	#Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path
	#Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)
	#Important: Form controls cannot be accessed in this function
	#TODO: Add modules and custom code to validate the application load
	
	return $true #return true for success or false for failure
}


function OnApplicationExit {
	#Note: This function is not called in Projects
	#Note: This function runs after the form is closed
	#TODO: Add custom code to clean up and unload modules when the application exits
	
	$script:ExitCode = 0 #Set the exit code for the Packager
}

$form1_Load={
	#TODO: Initialize Form Controls here
	
}

#region Control Helper Functions
function Load-DataGridView
{
	<#
	.SYNOPSIS
		This functions helps you load items into a DataGridView.

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

	.PARAMETER  DataGridView
		The ComboBox control you want to add items to.

	.PARAMETER  Item
		The object or objects you wish to load into the ComboBox's items collection.
	
	.PARAMETER  DataMember
		Sets the name of the list or table in the data source for which the DataGridView is displaying data.

	#>
	Param (
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		[System.Windows.Forms.DataGridView]$DataGridView,
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		$Item,
	    [Parameter(Mandatory=$false)]
		[string]$DataMember
	)
	$DataGridView.SuspendLayout()
	$DataGridView.DataMember = $DataMember
	
	if ($Item -is [System.ComponentModel.IListSource]`
	-or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
	{
		$DataGridView.DataSource = $Item
	}
	else
	{
		$array = New-Object System.Collections.ArrayList
		
		if ($Item -is [System.Collections.IList])
		{
			$array.AddRange($Item)
		}
		else
		{	
			$array.Add($Item)	
		}
		$DataGridView.DataSource = $array
	}
	
	$DataGridView.ResumeLayout()
}

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 "Red", "White", "Blue"
$buttonGo_Click={
	#TODO: Place custom script here
	Load-DataGridView -DataGridView $datagridview1 -Item $combobox1.SelectedItem
}
Last edited by seaeric on Mon Jul 07, 2014 1:19 pm, edited 1 time in total.
User avatar
pdearmen
Posts: 66
Last visit: Mon Mar 11, 2024 1:29 pm

Re: ComboBox - SelectedItem Displays Length Value

Post by pdearmen »

use .selecteditem.tostring()

so if your dropdown list is named $clientSelector you would use

$clientSelector.selecteditem.tostring()
User avatar
seaeric
Posts: 5
Last visit: Tue Jul 08, 2014 11:15 am

Re: ComboBox - SelectedItem Displays Length Value

Post by seaeric »

I have tried that. When I press the button, the results are the length of the variable chosen vs. the actual string value. If I remove the .tostring()method, the results are the same.
PowerShell Code
Double-click the code block to select all.
Load-DataGridView -DataGridView $datagridview1 -Item $combobox1.SelectedItem.ToString()
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: ComboBox - SelectedItem Displays Length Value

Post by davidc »

Load-DataGridView is going to display the properties of the selected object not the value itself.

David
David
SAPIEN Technologies, Inc.
User avatar
pdearmen
Posts: 66
Last visit: Mon Mar 11, 2024 1:29 pm

Re: ComboBox - SelectedItem Displays Length Value

Post by pdearmen »

Ah yes - thanks -I missed that little line in the above code
This topic is 9 years and 8 months 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.