Powershell Windows窗体多级组合框

时间:2016-03-31 09:47:38

标签: powershell combobox

我有一个内置在PowerShell中的窗体,它设计用于提供3个组合框:

CB1:显示5个职位描述

CB2:当选择CB1中的任何选项时,使用$ ComboBox1_SelectedIndexChanged匹配,然后使用switch语句

CB3:当选择CB2中的任何选项时,使用$ ComboBox2_SelectedIndexChanged匹配,然后使用switch语句

在此图片中,我选择了"商业理事会"它显示了列表中的第一个匹配"分支属性信托"

enter image description here

这个问题是2折。

  1. 我不希望它在第二个组合框中显示任何内容,直到我选择一个(停止用户假设默认值)

  2. 我必须点击结果,然后重新打开它,让它在最后的组合框中显示结果,在这个例子中它显示"结果1,2,3正确"。

  3. example

    进一步的问题:

    1. 我不能使用组合框上的sort属性作为引用数据源,我可以用另一种方式对数据进行排序吗? (A-Z)

    2. 如果我填充所有3个框,如果我在CBO1中选择其他选项,我是否可以将表格重置第2和第3个组合框为空白? (基本上重置表格)

        $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 个答案:

答案 0 :(得分:1)

  1. 设置数据源后,您可以执行以下操作:

    $combobox2.Text=""
    
    1. 也解决了这个问题
  2. 进一步的问题:

    1. 您可以像这样对数据源进行排序:

      $envnames = $envnames | sort
      

      这必须在将值分配给变量之后但在将其设置为组合框的数据源之前完成。

    2. 要重置其他组合框,您只需将数据源设置为$combobox1_selectedindexchanged块开头的空数组:

      $ComboBox1_SelectedIndexChanged=
      {
          $comboBox2.DataSource=@()
          $combobox2.Text = ""
          $combobox3.DataSource=@()
          $combobox3.Text = ""
      ....
      }
      
    3. 请注意,我不使用Windows窗体,这就是我在2分钟内一起攻击的内容,可能不是最佳解决方案