在Powershell中定义多个下拉选择器

5kgi1eie  于 2023-02-04  发布在  Shell
关注(0)|答案(2)|浏览(144)

我正在使用PowerShell,我需要定义81个下拉选择框,它们都是相同的,但能够读回81个不同的结果。

  • foreach* 循环似乎是最明显的途径,但我尝试过:
Foreach ($Puzzleleitem in $InitialPuzzle)
        {
        $dropdowntest[$Puzzleleitem] =new-object System.Windows.Forms.ComboBox 
        }

但我得到了以下错误:

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
uujelgoq

uujelgoq1#

示例摘自上述评论中的相关问题:

# 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++
}
kfgdxczn

kfgdxczn2#

我整理了一些资料,找到了一个完整的解决方案,下面将定义一个表单,在其中放置81个均匀分布的下拉框,并在变量“value$i”中获取结果,其中$1是一个数字。

# 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()

https://grumpy.tech/powershell-recursive-dropdown-boxes/中对代码有更深入的解释

相关问题