2

Attempting to import .csv cell contents into a PowerShell Form Combo/Drop Down Box which works when I hard code in each variable number ($DropDownBox1.Text, $DropDownBox2.Text etc.) but was looking to make it a bit smarter and loop through each instance and import appropriately.

Is it possible to modify the left hand assignment to get it to increment accordingly?

$i=0
$x=1
Foreach($User in $ISDept)
{
    $DropDownBox[$x].Text = Import-Csv -Path $File | Select-object -Index ($i) |% {$_.Status}
    $i++
    $x++
}

Using the above code I get the error:

"Unable to index into an object of type System.Windows.Forms.ComboBox."

Thanks in advance.

VernonW
  • 91
  • 8

1 Answers1

1

I presume you have some code that creates all dropdown boxes, with names as you described. In that case, you need to use dynamic variable and Get-Variable command. Try this

$i=0
$x=1
Foreach($User in $ISDept)
{
    $DropDownBox = Get-Variable -Name ('DropDownBox' + ([string]$x)) | Select -Expand Value
    $DropDownBox.Text = Import-Csv -Path $File | Select-object -Index ($i) |% {$_.Status}
    $i++
    $x++
}
Igor
  • 1,349
  • 12
  • 25
  • Perfect! I had tried the get-variable function but my syntax when trying to join the 'DropDownBox' string and incrementing number was a bit off!? Thanks again. – VernonW Jun 30 '17 at 06:35