问题
I have a windows form built in powershell thats designed to give 3 comboboxes:
CB1: Displays 5 Job descriptions
CB2: When any option from CB1 is selected its matched using $ComboBox1_SelectedIndexChanged and then a switch statement
CB3: When any option from CB2 is selected its matched using $ComboBox2_SelectedIndexChanged and then a switch statement
In this image, i have selected "Commercial Directorate" and its showing me the first match in the list "branch property trusts"
enter image description here
The issue with this is 2 fold.
i dont want it to show anything in this second combo box until i select one (stops users assuming defaults)
I have to click off the result and then back on it to have it display a result in the final combo box, in this example it shows "Result 1,2,3 correctly".
example
Further questions:
i cant use the sort property on the combobox as its referencing a datasource, can i sort the data another way ? (A-Z)
If i populate all 3 boxes, can i get the form to reset the 2nd and 3rd combo boxes to blank if i select a different option in CBO1? (essentially reset form)
$initialForm = New-Object System.Windows.Forms.Form $initialForm.Size = New-Object System.Drawing.Size(300,300) $descriptions = @("Select Item","Commercial Directorate","DG Directorate","Financial Directorate","Fundraising Directorate","HR Directorate") $ComboBox1_SelectedIndexChanged= { Switch ($comboBox1.text) { "Commercial Directorate" { $envnames = @("Branch Property Trusts Programme") } "DG Directorate" { $envnames = @("Director General","Governance & Administration") } default { $envnames = @() } } $comboBox2.Remove_SelectedIndexChanged($ComboBox2_SelectedIndexChanged) $comboBox2.DataSource = $envnames $ComboBox2.add_SelectedIndexChanged($ComboBox2_SelectedIndexChanged) } $ComboBox2_SelectedIndexChanged= { Switch ($comboBox2.text) { "Branch Property Trusts Programme" { $envnames2 = @("Result1","Result2","Result3") } "Director General" { $envnames2 = @("Result4","Result5") } default { $envnames2 = @() } } $comboBox3.Remove_SelectedIndexChanged($ComboBox2_SelectedIndexChanged) $comboBox3.DataSource = $envnames2 $ComboBox3.add_SelectedIndexChanged($ComboBox2_SelectedIndexChanged) } $comboBox1 = New-Object System.Windows.Forms.ComboBox $comboBox1.Location = New-Object System.Drawing.Point(25, 90) $comboBox1.Size = New-Object System.Drawing.Size(180, 20) $comboBox1.DataSource = $descriptions $ComboBox1.add_SelectedIndexChanged($ComboBox1_SelectedIndexChanged) $comboBox2 = New-Object System.Windows.Forms.ComboBox $comboBox2.Location = New-Object System.Drawing.Point(25, 120) $comboBox2.Size = New-Object System.Drawing.Size(180, 20) $ComboBox2.add_SelectedIndexChanged($ComboBox2_SelectedIndexChanged) $comboBox3 = New-Object System.Windows.Forms.ComboBox $comboBox3.Location = New-Object System.Drawing.Point(25, 150) $comboBox3.Size = New-Object System.Drawing.Size(180, 20) $initialForm.Controls.Add($comboBox1) $initialForm.Controls.Add($comboBox2) $initialForm.Controls.Add($comboBox3) $initialForm.ShowDialog()
回答1:
After setting the Datasource you can do:
$combobox2.Text=""- solves this problem too
Further questions:
You can just sort your datasource like this:
$envnames = $envnames | sortThis has to be done after assigning the values to the variable but before setting it as a datasource for your combobox.
To reset your other Comboboxes you could just set the Datasource to an empty array at the beginning of your
$combobox1_selectedindexchangedblock:$ComboBox1_SelectedIndexChanged= { $comboBox2.DataSource=@() $combobox2.Text = "" $combobox3.DataSource=@() $combobox3.Text = "" .... }
Note that i dont work with windows forms so thats just what i hacked together in 2 minutes, might not be the optimal solution
来源:https://stackoverflow.com/questions/36329829/powershell-windows-form-multilevel-combo-box