首页 > 解决方案 > 从另一个列表框中的 selected.item 填充列表框

问题描述

祝大家有美好的一天!

我正在为我的公司制作一个 Powershell 程序。我有 2 个列表框,一个是打印机品牌,另一个是打印机型号。我希望帮助用用户在“制造”列表中选择的内容填充“模型”列表。我知道做一个选定的项目可以工作,但无论我做什么,它都不会填充“模型”列表。

这是我第一次编程,是自学的。现在,我正在使用 Windows.Forms 来制作 GUI。

任何帮助表示赞赏。非常感谢这个志趣相投的精彩社区!

*代码:

Add-Type -AssemblyName System.Windows.Forms
$Form_Service = New-Object system.Windows.Forms.Form
$Form_Service.ClientSize = '452,400'
$Form_Service.text = "Service Call"
$Form_Service.TopMost = $true
$Form_Service.StartPosition = 'CenterScreen'

$Label_ValleyID = New-Object system.Windows.Forms.Label
$Label_ValleyID.text = "Enter Valley ID"
$Label_ValleyID.AutoSize = $true
$Label_ValleyID.width= 25
$Label_ValleyID.height = 10
$Label_ValleyID.location = New-Object System.Drawing.Point(45,41)
$Label_ValleyID.Font = 'Microsoft Sans Serif,10'

$TextBox_ValleyID= New-Object system.Windows.Forms.TextBox
$TextBox_ValleyID.multiline= $false
$TextBox_ValleyID.width= 180
$TextBox_ValleyID.height = 20
$TextBox_ValleyID.location = New-Object System.Drawing.Point(45,62)
$TextBox_ValleyID.Font = 'Microsoft Sans Serif,10'

$Label_Make= New-Object system.Windows.Forms.Label
$Label_Make.text = "Make"
$Label_Make.AutoSize = $true
$Label_Make.width= 25
$Label_Make.height = 10
$Label_Make.location = New-Object System.Drawing.Point(45,108)
$Label_Make.Font = 'Microsoft Sans Serif,10'

$ListBox_Make= New-Object system.Windows.Forms.ListBox
$ListBox_Make.text = "Make"
$ListBox_Make.width= 144
$ListBox_Make.height = 50
$ListBox_Make.location = New-Object System.Drawing.Point(45,129)

[void] $ListBox_Make.Items.Add('Brother')
[void] $ListBox_Make.Items.Add('Canon')
[void] $ListBox_Make.Items.Add('HP')
[void] $ListBox_Make.Items.Add('Kyocera')
[void] $ListBox_Make.Items.Add('Ricoh')
[void] $ListBox_Make.Items.Add('Sharp')

$Label_Model = New-Object system.Windows.Forms.Label
$Label_Model.text= "Model"
$Label_Model.AutoSize= $true
$Label_Model.width = 25
$Label_Model.height= 10
$Label_Model.location= New-Object System.Drawing.Point(259,108)
$Label_Model.Font= 'Microsoft Sans Serif,10'

$ListBox_Model = New-Object system.Windows.Forms.ListBox
$ListBox_Model.text= "Model"
$ListBox_Model.width = 146
$ListBox_Model.height= 50
$ListBox_Model.location= New-Object System.Drawing.Point(259,129)

$Label_Location= New-Object system.Windows.Forms.Label
$Label_Location.text = "Location"
$Label_Location.AutoSize = $true
$Label_Location.width= 25
$Label_Location.height = 10
$Label_Location.location = New-Object System.Drawing.Point(45,195)
$Label_Location.Font = 'Microsoft Sans Serif,10'

$TextBox_Location= New-Object system.Windows.Forms.TextBox
$TextBox_Location.multiline= $false
$TextBox_Location.width= 363
$TextBox_Location.height = 20
$TextBox_Location.location = New-Object System.Drawing.Point(45,215)
$TextBox_Location.Font = 'Microsoft Sans Serif,10'

$Label_Problem = New-Object system.Windows.Forms.Label
$Label_Problem.text= "State what is wrong:"
$Label_Problem.AutoSize= $true
$Label_Problem.width = 25
$Label_Problem.height= 10
$Label_Problem.location= New-Object System.Drawing.Point(45,250)
$Label_Problem.Font= 'Microsoft Sans Serif,10'

$TextBox_Problem = New-Object system.Windows.Forms.TextBox
$TextBox_Problem.multiline = $false
$TextBox_Problem.width = 364
$TextBox_Problem.height= 100
$TextBox_Problem.location= New-Object System.Drawing.Point(45,270)
$TextBox_Problem.Font= 'Microsoft Sans Serif,10'

$CheckBox_Nope = New-Object system.Windows.Forms.CheckBox
$CheckBox_Nope.text= "Is your printer inoperable?"
$CheckBox_Nope.width = 250
$CheckBox_Nope.height= 50 
$CheckBox_Nope.location= New-Object System.Drawing.Point(145,295)
$CheckBox_Nope.Font = 'Microsoft Sans Serif,10'

$Button_Submit = New-Object system.Windows.Forms.Button
$Button_Submit.text= "Submit"
$Button_Submit.width = 70
$Button_Submit.height= 30
$Button_Submit.location= New-Object System.Drawing.Point(189,345)
$Button_Submit.Font= 'Microsoft Sans Serif,10'

$Form_Service.controls.AddRange(@($Label_ValleyID,$Label_Make,$Label_Model,$Label_Location,$Label_Problem,$TextBox_ValleyID,$ListBox_Make,$ListBox_Model,$TextBox_Location,$TextBox_Problem,$Button_Submit,$CheckBox_Nope))

if ($ListBox_Make.SelectedItem -eq "Brother"){
[void] $ListBox_Model.Items.Add('MP301')
}

[void]$Form_Service.ShowDialog()

标签: winformspowershelllistbox

解决方案


好的,所以您要查找的内容称为事件。事件是发生的动作,然后允许您在动作之后运行代码。就像鼠标单击对象或按下键盘按钮时一样。

在 Powershell 中处理 Winforms 时,您可以使用

$Control.Add_EventName{
    Code Here
}

在已经调用控件之后放置事件。我通常把它们放在我展示表格之前。

$ListBox_Make.Add_Click{
    $ListBox_Model.Items.Add($ListBox_Make.SelectedItem)
}

[void]$Form_Service.ShowDialog()

在您的确切情况下,您可以使用:

$ListBox_Make.Add_Click{
    if ($ListBox_Make.SelectedItem -eq "Brother"){
        [void] $ListBox_Model.Items.Add('MP301')
    }
}

希望这能让你走上正确的轨道。

也不是一堆 if 语句,而是尝试一个 Switch

$ListBox_Make.Add_Click{
    switch ($ListBox_Make.SelectedItem){
        "Brother"{
            $ListBox_Model.Items.Add('MP301')
        }
        "Canon"{
            $ListBox_Model.Items.Add('LT45')
        }
        "HP"{
            $ListBox_Model.Items.Add('ABC2133')
        }
    }
}

您可以找到列表框的事件 https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox?view=netframework-4.7.2#events


推荐阅读