Powershell Windows Form Multilevel Combo Box

无人久伴 提交于 2019-12-13 01:26:42

问题


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.

  1. i dont want it to show anything in this second combo box until i select one (stops users assuming defaults)

  2. 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:

  1. i cant use the sort property on the combobox as its referencing a datasource, can i sort the data another way ? (A-Z)

  2. 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:


  1. After setting the Datasource you can do:

    $combobox2.Text=""
    
    1. solves this problem too

Further questions:

  1. You can just sort your datasource like this:

    $envnames = $envnames | sort
    

    This has to be done after assigning the values to the variable but before setting it as a datasource for your combobox.

  2. To reset your other Comboboxes you could just set the Datasource to an empty array at the beginning of your $combobox1_selectedindexchanged block:

    $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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!