我有一个表格,上面有 3 个组合框。第一个组合框从 sql 中提取数据。获得数据后,我希望第二个组合框根据第一个组合框的选择自动填充,最后最后一个组合框将自动填充,允许用户从预定义列表中选择一个选项。
因此,如果您在第一个组合框中选择“水果”
有人能把这些组合框连接起来吗?基本脚本如下:
[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
所有你需要做的是为选择的项目变更添加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)
}
}
}