powershell populate combobox basing on the selected item on another combobox

本小妞迷上赌 提交于 2021-01-28 20:27:59

问题


This time I am stuck with another PowerShell GUI challenge. I have a Form that contains two different comboboxes (combobox1 and combobox2)

What I want to do is: The first combobox to show a list of all the clients that I have and the second combobox to show all the different mailboxes that are available for the client that has been selected on the first combobox.

Once again I don't know how to do it so I am asking you guys for advise.

I managed to show a list of all the clients on the first combobox. But I have never managed to populate the second combobox showing the different mailboxes available for that specific client.

This is the code I've got so far, the issue is that I don't know how to do to "trigger the population of the second combobox"

$MainForm_Load={
#TODO: Initialize Form Controls here
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010 -ea silentlycontinue
import-module activedirectory

$Clients = Get-ADOrganizationalUnit -SearchBase 'OU=Clients,DC=asp,DC=xaracloud,DC=net' -SearchScope Onelevel -Filter * -Properties Description

foreach ($client in $Clients)
{
    $CurrentClient = "{0} ({1})" -f $client.Name, $client.Description
    Load-ComboBox $combobox1 $CurrentClient -Append
}

if ($combobox1.SelectedIndex -gt -1)
{

    $ClientSelected = ($combobox1.SelectedItem) -replace " \(.*\)"

    $Mailboxes = Get-Mailbox -OrganizationalUnit $ClientSelected
    foreach ($mailbox in $Mailboxes)
    {
        $CurrentMailbox = "{0} ({1})" -f $mailbox.Name, $mailbox.Alias
        Load-ComboBox $combobox2 $CurrentMailbox -Append
    }

}

}

Please note that in order to populate the second combobox basing on the selection of the first one I have to query my exchange server and that takes a few seconds. Is there any way of showing a process bar to say that the request is being processed? I don't want the user to think that the script is doing nothing or that it doesn't work.


回答1:


Remove the | Out-String from the Get-Mailbox line and add use one of this options(of course there's more):

Use a ComboBox.SelectionChangeCommitted Event:

"Occurs when the user changes the selected item and that change is displayed in the ComboBox"

$combobox2_SelectionChangeCommitted={

  $Mailboxes = Get-Mailbox -OrganizationalUnit $ClientSelected
  foreach ($mailbox in $Mailboxes)
  {
      $CurrentMailbox = "{0} ({1})" -f $mailbox.Name, $mailbox.Alias
      Load-ComboBox $combobox2 $CurrentMailbox -Append
  }

}

Use a button:

$button1_Click={

$Mailboxes = Get-Mailbox -OrganizationalUnit $ClientSelected
  foreach ($mailbox in $Mailboxes)
  {
      $CurrentMailbox = "{0} ({1})" -f $mailbox.Name, $mailbox.Alias
      Load-ComboBox $combobox2 $CurrentMailbox -Append
  }
}

Also, there's more options, you can start with one of the above...



来源:https://stackoverflow.com/questions/33420948/powershell-populate-combobox-basing-on-the-selected-item-on-another-combobox

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