0

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
2
  • Where is $dropdowntest defined? The error indicates you've already done $dropdowntest = New-Object System.Windows.Forms.ComboBox earlier in the script :) Commented Jan 13, 2023 at 13:11
  • 1
    This previous question should point you in the right direction: stackoverflow.com/questions/65697153/… Commented Jan 13, 2023 at 13:18

2 Answers 2

1

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++
}
Sign up to request clarification or add additional context in comments.

1 Comment

Its 90% there. I can work out the rest. Thanks
0

I managed to collate a few sources and come up with a full solution for my question. The following will define a form and place 81 evenly spaced drop down boxes in it and capture the results in the variables "value$i" where $1 is a number.

enter image description here

    # Set the size of your form
        $Form = New-Object System.Windows.Forms.Form
        $Form.Width = 600
        $Form.height = 600
        $Form.Text = ”Mat Grumps dropdown boxes example”
    
    # Set the font of the text to be used within the form
        $Font = New-Object System.Drawing.Font("Ariel",8)
        $Form.Font = $Font
    
    #Set some valus to set starting points
        $i= 0
        $x=30
        $y=30
    
    #Now we create 81 dropdown boxes
     1..81 | ForEach-Object {"ComboBox$PSitem"} | ForEach{
            $CurrentObj = $null
            $CurrentObj = New-Object System.Windows.Forms.ComboBox
    
    #increment position
        if ((($i/9) -is [int]) -and ($i -gt 0))
            {
                $y=$y + 50
                $x=$x - 540    
            }
    
    #Set location of the boxes
        $CurrentObj.Location = new-object System.Drawing.Size($x,($y))
    #Set Size
        $CurrentObj.Size = new-object System.Drawing.Size (55,40)
        #define selections
         @('blank',‘1’,’2’,’3',‘4’,’5’,’6’,‘7’,’8’,’9’) | ForEach-Object {[void] $CurrentObj.Items.Add($_)}
    
    #Set default selection
        $CurrentObj.SelectedIndex = 0
    
    #Create dynamic variables to store choices
    #Make sure the variable doesnt exist already
        Remove-Variable -Name "value$i" -ErrorAction Ignore
    #Create the dynamic variable
        New-Variable "value$i" $CurrentObj
    
        #add the buttons to the form
        $form.Controls.Add($CurrentObj)
            
        #increment some stuff
        $x = $x + 60
        $i++
        }
    
        #Draw the form and dropdown boxes
        $form.ShowDialog()

There is a deeper explanation of the code here https://grumpy.tech/powershell-recursive-dropdown-boxes/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.