Powershell连接Combobox teird

时间:2014-07-22 12:33:10

标签: powershell combobox

我有一个有3个组合框的表格。第一个组合框从sql中提取数据。一旦有了数据,我希望第二个组合框根据第一个组合框选择自动填充,最后最后一个组合框将自动填充,允许用户从预定义列表中选择一个选项。

所以如果你选择"水果"在第一个组合框中>第二个组合框将有选项apple>等等。 在哪里选择蔬菜>你会得到黄瓜>等等。

是否有人能够将这些组合框链接起来。基本脚本如下:

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")  





$Form1 = New-Object System.Windows.Forms.Form   
    $Form1.ClientSize = New-Object System.Drawing.Size(600, 313)
    $Form1.Text = $ApplicationVersion
    $Form1.BackgroundImage = $Image
    $Form1.BackgroundImageLayout = "stretch"
    $Form1.FormBorderStyle = "FixedDialog"
    $Form1.Icon = $Icon
    #~~<Hide Console >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #Hide-Console
    #~~<Window  State >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#~~< Label CLASSIFICATION 1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label_Classification = New-Object System.Windows.Forms.Label
    $Label_Classification.Location = New-Object System.Drawing.Point(295, 83)
    $Label_Classification.Size = New-Object System.Drawing.Size(95, 23)
    $Label_Classification.TabIndex = 11
    $Label_Classification.Text = "Classification T1"
    $Label_Classification.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
    $Label_Classification.BackColor = "Transparent"
    #~~< Label CLASSIFICATION 2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label_Classification2 = New-Object System.Windows.Forms.Label
    $Label_Classification2.Location = New-Object System.Drawing.Point(295, 113)
    $Label_Classification2.Size = New-Object System.Drawing.Size(95, 23)
    $Label_Classification2.TabIndex = 11
    $Label_Classification2.Text = "Classification T2"
    $Label_Classification2.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
    $Label_Classification2.BackColor = "Transparent"
    #~~< Label CLASSIFICATION 3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Label_Classification3 = New-Object System.Windows.Forms.Label
    $Label_Classification3.Location = New-Object System.Drawing.Point(295, 143)
    $Label_Classification3.Size = New-Object System.Drawing.Size(95, 23)
    $Label_Classification3.TabIndex = 11
    $Label_Classification3.Text = "Classification T3"
    $Label_Classification3.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
    $Label_Classification3.BackColor = "Transparent"
    #~~< ComboBox CLASSIFICATION 1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Classarray = @("Fruit","Vegtable")
    $Tier1ClassificationCategory = New-Object System.Windows.Forms.ComboBox
    $Tier1ClassificationCategory.Location = New-Object System.Drawing.Point(400, 83)
    $Tier1ClassificationCategory.SelectedIndex = -1
    $Tier1ClassificationCategory.Size = New-Object System.Drawing.Size(170, 21)
    $Tier1ClassificationCategory.TabIndex = 5
    ForEach ($Class in $Classarray) {
    $Tier1ClassificationCategory.Items.Add($Class)
    } #end foreach
    #~~< ComboBox CLASSIFICATION 2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Classarray2 = @("Apple","Bananna","Pear","Tomatoe","Cucumber","Pepper")
    $Tier2ClassificationCategory = New-Object System.Windows.Forms.ComboBox
    $Tier2ClassificationCategory.Location = New-Object System.Drawing.Point(400, 113)
    $Tier2ClassificationCategory.SelectedIndex = -1
    $Tier2ClassificationCategory.Size = New-Object System.Drawing.Size(170, 21)
    $Tier2ClassificationCategory.TabIndex = 6
    foreach ($Class in $Classarray2) {
    $Tier2ClassificationCategory.Items.Add($Class)
     } #end foreach  
    #~~< ComboBox CLASSIFICATION 3 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $Classarray3 = @("Green Apple","Red Apple","Green Tomatoe","Yellow Pepper")
    $Tier3ClassificationCategory = New-Object System.Windows.Forms.ComboBox
    $Tier3ClassificationCategory.Location = New-Object System.Drawing.Point(400, 143)
    $Tier3ClassificationCategory.SelectedIndex = -1
    $Tier3ClassificationCategory.Size = New-Object System.Drawing.Size(170, 21)
    $Tier3ClassificationCategory.TabIndex = 7
    foreach ($Class in $Classarray3) {
    $Tier3ClassificationCategory.Items.Add($Class)
     } #end foreach



$Form1.Controls.Add($From1_Title)
    $Form1.Controls.Add($Label_Title)
    $Form1.Controls.Add($Label_Classification)
    $Form1.Controls.Add($Label_Classification2)
    $Form1.Controls.Add($Label_Classification3)
    $Form1.Controls.Add($Tier1ClassificationCategory)
    $Form1.Controls.Add($Tier2ClassificationCategory)
    $Form1.Controls.Add($Tier3ClassificationCategory)




    $Form1.ShowDialog() |Out-Null

1 个答案:

答案 0 :(得分:1)

您需要做的就是为Selected Item Change添加2个处理程序。

因此,在将控件添加到表单之前,请添加这两行

$Tier1ClassificationCategory.add_SelectedIndexChanged({Tier1ClassificationCategory_SelectedIndexChanged})
$Tier2ClassificationCategory.add_SelectedIndexChanged({Tier2ClassificationCategory_SelectedIndexChanged}) 

在脚本的开头定义选择索引更改时将运行的函数。例如:

    $Fruits = @('Apple','Banana','Pear')       
    $Veggies = @('Tomato','Cucumber','Pepper')

Function Tier1ClassificationCategory_SelectedIndexChanged {
    if ($Tier1ClassificationCategory.selectedItem -eq "Fruit") {             
         $Tier2ClassificationCategory.Items.Clear()
         foreach ($f in $Fruits) {
            $Tier2ClassificationCategory.Items.Add($veggie)
         }

    } else {
         $Tier2ClassificationCategory.Items.Clear()
            foreach ($f in $Fruits) {
            $Tier2ClassificationCategory.Items.Add($f)
         }


    }
}