Home > Mobile >  Define multiple dropdown selectors in Powershell
Define multiple dropdown selectors in Powershell

Time:01-14

I am using PowerShell and I need to define 81 dropdown selector boxes that are all the same but be able to read back 81 different results. My issue is with creating the dropdowns.

A foreach loop seems the most obvious route but I tried :

  Foreach ($Puzzleleitem in $InitialPuzzle)
        {
        $dropdowntest[$Puzzleleitem] =new-object System.Windows.Forms.ComboBox 
        } 

But I get the following error:

Unable to index into an object of type System.Windows.Forms.ComboBox.
At line:3 char:9
          $dropdowntest[$Puzzleleitem] =new-object System.Windows.Forms ...
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      FullyQualifiedErrorId : CannotIndex

CodePudding user response:

Example taken from linked question in comments above:

# Get the number of combo boxes required:
$count = $InitialPuzzle.Count

1..$count | ForEach-Object {"ComboBox$PSitem"} | ForEach{
    $CurrentObj             = $null

    $CurrentObj             = New-Object System.Windows.Forms.ComboBox
    # Increment location values
    $CurrentObj.Location    = "125,$(100 50*$i)"
    $CurrentObj.Size        = '100,35'
    $CurrentObj.Text        = $PSItem
    # Uncomment the following to add the button to $form
    # $form.Controls.Add($CurrentObj)
    # Just for info:
    Write-Host $CurrentObj.Text $CurrentObj.Location
    #
    $i  
}
  • Related