Trigger an event in a form from a drop down list

I’m trying to create automation form to create an AD user. We have 2 different companies listed in a drop down menu. I would like the content of the Listbox to change based on the selection of the combobox. Not sure if there is a way to do it.



[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  #loading the necessary .net libraries (using void to suppress output)

$form = New-Object System.Windows.Forms.Form 
$form.Text = "AD User Creation"
$form.Size = New-Object System.Drawing.Size(350,475) 
$form.StartPosition = "CenterScreen"
#################################################################################################################################################################################################### Start functions
function Create-newuser {
  

 
}
#################################################################################################################################################################################################### End Function

#################################################################################################################################################################################################### Start Buttons
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(40,400)
$OKButton.Size = New-Object System.Drawing.Size(90,23)
$OKButton.Text = "Create User"
$OKButton.Add_Click({Return-DropDown}) #the action triggered by the button
$Form.Controls.Add($OKButton) #activating the button inside the primary window

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(160,400)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
#################################################################################################################################################################################################### End Buttons

#################################################################################################################################################################################################### Start Label
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(10,10) 
$labelClub.Size = New-Object System.Drawing.Size(280,20) 
$labelClub.Text = "Please Select the Company"
$form.Controls.Add($labelClub)

$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(10,80) 
$labelClub.Size = New-Object System.Drawing.Size(280,20) 
$labelClub.Text = "Please Select the Club"
$form.Controls.Add($labelClub)

$labelFirst = New-Object System.Windows.Forms.Label
$labelFirst.Location = New-Object System.Drawing.Point(10,200) 
$labelFirst.Size = New-Object System.Drawing.Size(65,20) 
$labelFirst.Text = "First Name:"
$form.Controls.Add($labelFirst)

$labelLast = New-Object System.Windows.Forms.Label
$labelLast.Location = New-Object System.Drawing.Point(10,230) 
$labelLast.Size = New-Object System.Drawing.Size(65,20) 
$labelLast.Text = "Last Name:"
$form.Controls.Add($labelLast)

$labelUser = New-Object System.Windows.Forms.Label
$labelUser.Location = New-Object System.Drawing.Point(10,260) 
$labelUser.Size = New-Object System.Drawing.Size(65,20) 
$labelUser.Text = "Username:"
$form.Controls.Add($labelUser)

$labelTitle = New-Object System.Windows.Forms.Label
$labelTitle.Location = New-Object System.Drawing.Point(10,290) 
$labelTitle.Size = New-Object System.Drawing.Size(65,20) 
$labelTitle.Text = "Job Title:"
$form.Controls.Add($labelTitle)

$labelManager = New-Object System.Windows.Forms.Label
$labelManager.Location = New-Object System.Drawing.Point(10,320) 
$labelManager.Size = New-Object System.Drawing.Size(65,20) 
$labelManager.Text = "Manager:"
$form.Controls.Add($labelManager)
#################################################################################################################################################################################################### End Label

#################################################################################################################################################################################################### Start CheckBox
 $checkboxEmail = new-object System.Windows.Forms.checkbox
 $checkboxEmail.Location = new-object System.Drawing.Size(30,350)
 $checkboxEmail.Size = new-object System.Drawing.Size(55,45)
 $checkboxEmail.Text = "Email"
 $checkboxEmail.Checked = $false
 $form.Controls.Add($checkboxEmail)  

 $checkboxWeb = new-object System.Windows.Forms.checkbox
 $checkboxWeb.Location = new-object System.Drawing.Size(85,350)
 $checkboxWeb.Size = new-object System.Drawing.Size(100,45)
 $checkboxWeb.Text = "Web Acess"
 $checkboxWeb.Checked = $false
 $form.Controls.Add($checkboxWeb)  
#################################################################################################################################################################################################### End CheckBox

#################################################################################################################################################################################################### Start ListBox

$listBoxClub = New-Object System.Windows.Forms.ListBox 
$listBoxClub.Location = New-Object System.Drawing.Size(10,100) 
$listBoxClub.Size = New-Object System.Drawing.Size(260,20) 
$listBoxClub.Height = 80




#################################################################################################################################################################################################### End ListBox

#################################################################################################################################################################################################### Start ComboBox

$arrayCompany=@("Company1","Company2")

$comboBoxCompany = New-Object System.Windows.Forms.ComboBox 
$comboBoxCompany.Location = New-Object System.Drawing.Size(10,40) 
$comboBoxCompany.Size = New-Object System.Drawing.Size(180,20) 
$comboBoxCompany.DropDownHeight = 200 
$Form.Controls.Add($comboBoxCompany) 

foreach ($company in $arraycompany) {
                      $comboBoxCompany.Items.Add($company)
                              } 

$comboBoxCompany_SelectedIndexChanged=
{
   If ($comboBoxCompany.text -eq "Company1") 
   {
[void] $listBoxClub.Items.Add("site1")
[void] $listBoxClub.Items.Add("site2")
[void] $listBoxClub.Items.Add("site3")
[void] $listBoxClub.Items.Add("site4")
[void] $listBoxClub.Items.Add("site5")
[void] $listBoxClub.Items.Add("site6")
[void] $listBoxClub.Items.Add("site7")
[void] $listBoxClub.Items.Add("site8")
write-host "company1"

 }
   Else 
   {
[void] $listBoxClub.Items.Add("site9")
[void] $listBoxClub.Items.Add("site10")
[void] $listBoxClub.Items.Add("site11")
[void] $listBoxClub.Items.Add("site12")
[void] $listBoxClub.Items.Add("site13")
[void] $listBoxClub.Items.Add("site14")
[void] $listBoxClub.Items.Add("site15")
[void] $listBoxClub.Items.Add("site16")
write-host "company2"
 
   }
   $form.Controls.Add($listBoxClub)
}
################ MUST CREATE BEFORE ASSIGN ################
$comboBoxCompany.add_SelectedIndexChanged($comboBoxCompany_SelectedIndexChanged)

#################################################################################################################################################################################################### End ComboBox

#################################################################################################################################################################################################### Start Text Fields
$textBoxFirst = New-Object System.Windows.Forms.TextBox 
$textBoxFirst.Location = New-Object System.Drawing.Point(100,200) 
$textBoxFirst.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxFirst)

$textBoxLast = New-Object System.Windows.Forms.TextBox 
$textBoxLast.Location = New-Object System.Drawing.Point(100,230) 
$textBoxLast.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxLast)

$textBoxlUser = New-Object System.Windows.Forms.TextBox 
$textBoxlUser.Location = New-Object System.Drawing.Point(100,260) 
$textBoxlUser.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxlUser)

$textBoxlTitle = New-Object System.Windows.Forms.TextBox 
$textBoxlTitle.Location = New-Object System.Drawing.Point(100,290) 
$textBoxlTitle.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxlTitle)

$textBoxlManager = New-Object System.Windows.Forms.TextBox 
$textBoxlManager.Location = New-Object System.Drawing.Point(100,320) 
$textBoxlManager.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxlManager)

#################################################################################################################################################################################################### End Text Fields
$form.Add_Shown({$textBox.Select()})
[void] $form.ShowDialog()

Ok I figured it out.

Here is the updated script if can help anyone else.


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  #loading the necessary .net libraries (using void to suppress output)

$form = New-Object System.Windows.Forms.Form 
$form.Text = "AD User Creation"
$form.Size = New-Object System.Drawing.Size(350,475) 
$form.StartPosition = "CenterScreen"
#################################################################################################################################################################################################### Start functions
function Create-newuser {
  

 
}
#################################################################################################################################################################################################### End Function

#################################################################################################################################################################################################### Start Buttons
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(40,400)
$OKButton.Size = New-Object System.Drawing.Size(90,23)
$OKButton.Text = "Create User"
$OKButton.Add_Click({Return-DropDown}) #the action triggered by the button
$Form.Controls.Add($OKButton) #activating the button inside the primary window

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(160,400)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
#################################################################################################################################################################################################### End Buttons

#################################################################################################################################################################################################### Start Label
$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(10,10) 
$labelClub.Size = New-Object System.Drawing.Size(280,20) 
$labelClub.Text = "Please Select the Company"
$form.Controls.Add($labelClub)

$labelClub = New-Object System.Windows.Forms.Label
$labelClub.Location = New-Object System.Drawing.Point(10,80) 
$labelClub.Size = New-Object System.Drawing.Size(280,20) 
$labelClub.Text = "Please Select the Club"
$form.Controls.Add($labelClub)

$labelFirst = New-Object System.Windows.Forms.Label
$labelFirst.Location = New-Object System.Drawing.Point(10,200) 
$labelFirst.Size = New-Object System.Drawing.Size(65,20) 
$labelFirst.Text = "First Name:"
$form.Controls.Add($labelFirst)

$labelLast = New-Object System.Windows.Forms.Label
$labelLast.Location = New-Object System.Drawing.Point(10,230) 
$labelLast.Size = New-Object System.Drawing.Size(65,20) 
$labelLast.Text = "Last Name:"
$form.Controls.Add($labelLast)

$labelUser = New-Object System.Windows.Forms.Label
$labelUser.Location = New-Object System.Drawing.Point(10,260) 
$labelUser.Size = New-Object System.Drawing.Size(65,20) 
$labelUser.Text = "Username:"
$form.Controls.Add($labelUser)

$labelTitle = New-Object System.Windows.Forms.Label
$labelTitle.Location = New-Object System.Drawing.Point(10,290) 
$labelTitle.Size = New-Object System.Drawing.Size(65,20) 
$labelTitle.Text = "Job Title:"
$form.Controls.Add($labelTitle)

$labelManager = New-Object System.Windows.Forms.Label
$labelManager.Location = New-Object System.Drawing.Point(10,320) 
$labelManager.Size = New-Object System.Drawing.Size(65,20) 
$labelManager.Text = "Manager:"
$form.Controls.Add($labelManager)
#################################################################################################################################################################################################### End Label

#################################################################################################################################################################################################### Start CheckBox
 $checkboxEmail = new-object System.Windows.Forms.checkbox
 $checkboxEmail.Location = new-object System.Drawing.Size(30,350)
 $checkboxEmail.Size = new-object System.Drawing.Size(55,45)
 $checkboxEmail.Text = "Email"
 $checkboxEmail.Checked = $false
 $form.Controls.Add($checkboxEmail)  

 $checkboxWeb = new-object System.Windows.Forms.checkbox
 $checkboxWeb.Location = new-object System.Drawing.Size(85,350)
 $checkboxWeb.Size = new-object System.Drawing.Size(100,45)
 $checkboxWeb.Text = "Web Acess"
 $checkboxWeb.Checked = $false
 $form.Controls.Add($checkboxWeb)  
#################################################################################################################################################################################################### End CheckBox

#################################################################################################################################################################################################### Start ListBox

$listBoxClub = New-Object System.Windows.Forms.ListBox 
$listBoxClub.Location = New-Object System.Drawing.Size(10,100) 
$listBoxClub.Size = New-Object System.Drawing.Size(260,20) 
$listBoxClub.Height = 80




#################################################################################################################################################################################################### End ListBox

#################################################################################################################################################################################################### Start ComboBox

$arrayCompany=@("Company1","Company2")

$comboBoxCompany = New-Object System.Windows.Forms.ComboBox 
$comboBoxCompany.Location = New-Object System.Drawing.Size(10,40) 
$comboBoxCompany.Size = New-Object System.Drawing.Size(180,20) 
$comboBoxCompany.DropDownHeight = 200 
$Form.Controls.Add($comboBoxCompany) 

foreach ($company in $arraycompany) {
                      $comboBoxCompany.Items.Add($company)
                              } 

$comboBoxCompany_SelectedIndexChanged=
{
   If ($comboBoxCompany.text -eq "Company1") 
   {
   $listBoxClub.Items.Clear()
[void] $listBoxClub.Items.Add("site1")
[void] $listBoxClub.Items.Add("site2")
[void] $listBoxClub.Items.Add("site3")
[void] $listBoxClub.Items.Add("site4")
[void] $listBoxClub.Items.Add("site5")
[void] $listBoxClub.Items.Add("site6")
[void] $listBoxClub.Items.Add("site7")
[void] $listBoxClub.Items.Add("site8")
write-host "company1"
 }

   Else 
   {
   $listBoxClub.Items.Clear()
[void] $listBoxClub.Items.Add("site9")
[void] $listBoxClub.Items.Add("site10")
[void] $listBoxClub.Items.Add("site11")
[void] $listBoxClub.Items.Add("site12")
[void] $listBoxClub.Items.Add("site13")
[void] $listBoxClub.Items.Add("site14")
[void] $listBoxClub.Items.Add("site15")
[void] $listBoxClub.Items.Add("site16")
write-host "company2"
   }

$form.Controls.Add($listBoxClub)
}
$comboBoxCompany.add_SelectedIndexChanged($comboBoxCompany_SelectedIndexChanged)

#################################################################################################################################################################################################### End ComboBox

#################################################################################################################################################################################################### Start Text Fields
$textBoxFirst = New-Object System.Windows.Forms.TextBox 
$textBoxFirst.Location = New-Object System.Drawing.Point(100,200) 
$textBoxFirst.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxFirst)

$textBoxLast = New-Object System.Windows.Forms.TextBox 
$textBoxLast.Location = New-Object System.Drawing.Point(100,230) 
$textBoxLast.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxLast)

$textBoxlUser = New-Object System.Windows.Forms.TextBox 
$textBoxlUser.Location = New-Object System.Drawing.Point(100,260) 
$textBoxlUser.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxlUser)

$textBoxlTitle = New-Object System.Windows.Forms.TextBox 
$textBoxlTitle.Location = New-Object System.Drawing.Point(100,290) 
$textBoxlTitle.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxlTitle)

$textBoxlManager = New-Object System.Windows.Forms.TextBox 
$textBoxlManager.Location = New-Object System.Drawing.Point(100,320) 
$textBoxlManager.Size = New-Object System.Drawing.Size(165,20) 
$form.Controls.Add($textBoxlManager)

#################################################################################################################################################################################################### End Text Fields
$form.Add_Shown({$textBox.Select()})
[void] $form.ShowDialog()

E,

A couple tips to simplify a couple things.

Instead of

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

you can say

Add-Type -AssemblyName "System.Drawing"
Add-Type -AssemblyName "System.Windows.Forms"

Instead of many of your New-Object statements, you can leverage PowerShell’s dynamic type conversion to make your script a little easier to write and a little easier to read. It will run a few milliseconds slower, but it’s a good trade off.

For example, instead of

$OKButton.Location = New-Object System.Drawing.Point(40,400)
$OKButton.Size = New-Object System.Drawing.Size(90,23)

you can use

$OKButton.Location = '40, 400'
$OKButton.Size     = '90, 23'

You will find this especially useful when you start using more complex properties. For example, with an anchor, instead of this

$OKButton.Anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Left -bor [System.Windows.Forms.AnchorStyles]::Right

you can use this

$OKButton.Anchor = 'Top, Bottom, Left, Right'

And instead of creating and configuring complex font objects, you can just do this

$labelLoading.Font = "Microsoft Sans Serif, 12pt, style=Bold"