Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

List combobox items based of higher tiered combobox selection

$
0
0

I'm trying to base combobox arrays of previously selected combobox .text.

So I have combo box 1 with an array of Building 1, Building 2, Building 3, Building 4 with a button next to it.

When the button is selected it then runs an if statement IF Combobox 1 equals Building 1 create a new combobox with an array of Rooms that are in building 1 it then creates the combo box.

This works good!

The problem is when I to back to combobox 1 and select Building 2 and click the button I still have the same options of rooms from building 1 in combobox 2.

I've tried deleted in control, refreshing the form and adding a new control then refreshing the form when the button is clicked but I can't seem to get that initial array out of combobox 2 and replaced with an array of the new drop downs based on the first combobox.

Any suggestions? Script Below

 

#Verifies registry keys to support inventory exists, if they do not creates them values of 0
if((Get-ItemProperty HKLM:\Software\Microsoft -Name SomeCompany -ea 0).SomeCompany) {'Propertyalready exists'}
Else
{
New-Item -Path HKLM:\Software\Microsoft -Name SomeCompany -ea 0
}

if((Get-ItemProperty HKLM:\Software\Microsoft\SomeCompany -Name Building -ea 0).Building)  {'Propertyalready exists'}
Else
{
New-itemproperty HKLM:\Software\Microsoft\SomeCompany -name Building -value 0 -ea 0
}

if((Get-ItemProperty HKLM:\Software\Microsoft\SomeCompany -Name Lab -ea 0).Lab)  {'Propertyalready exists'}
Else
{
New-itemproperty HKLM:\Software\Microsoft\SomeCompany -name Lab -value 0 -ea 0
}

if((Get-ItemProperty HKLM:\Software\Microsoft\SomeCompany -Name Row -ea 0).Row)  {'Propertyalready exists'}
Else
{
New-itemproperty HKLM:\Software\Microsoft\SomeCompany -name Row -value 0 -ea 0
}

if((Get-ItemProperty HKLM:\Software\Microsoft\SomeCompany -Name Rack -ea 0).Rack)  {'Propertyalready exists'}
Else
{
New-itemproperty HKLM:\Software\Microsoft\SomeCompany -name Rack -value 0 -ea 0
}

if((Get-ItemProperty HKLM:\Software\Microsoft\SomeCompany -Name RackU -ea 0).RackU)  {'Propertyalready exists'}
Else
{
New-itemproperty HKLM:\Software\Microsoft\SomeCompany -name RackU -value 0 -ea 0
}

#Pulls reg entry data for the intial form
$CurrentBuildingKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).Building
$CurrentLabKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).Lab
$CurrentRowKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).Row
$CurrentRackKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).Rack
$CurrentRackuKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).RackU


# Load the Winforms assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# Create the form
$form = New-Object Windows.Forms.Form
$form.height=300
$form.width=850
$form.StartPosition= "CenterScreen"

# This line sets the default font for the form. [Font, Size, Style, ?] If no settins are present defaults are selected.

#Set the dialog title
$form.text = "Computer Location Entry"

# Create the label control and set text, size and location
$label = New-Object Windows.Forms.Label
$label.Location = New-Object Drawing.Point 50,25
$label.Size = New-Object Drawing.Point 4000,20
$label.text = "Review the current computer location information below, if any of the below information needs to be updated include in on the right side and click OK"

# Creates Combobox with size and location as well as array for the combobox
[array]$BuildingDropDownArray= "N/A","Building 1", "Building 2", "Building 3", "Building 3"
function Return-DropDown {
 $BuildingChoice=$BuildingDropDown.SelectedItem.ToString()
 $Form.Close()
 write-host $BuildingChoice
}
$BuildingDropDown=new-object System.Windows.Forms.Combobox
$BuildingDropDown.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
$BuildingDropDown.Location = New-Object System.Drawing.Size 445,75
$BuildingDropDown.Size = New-Object System.Drawing.Size 200,30
ForEach ( $building in $BuildingDropDownArray)
{
$BuildingDropDown.Items.Add($Building)
}

 

# Creates Lab for building row
$BuildingDescription = New-Object Windows.Forms.Label
$BuildingDescription.Location = New-Object Drawing.Point 380,77
$BuildingDescription.Size = New-Object Drawing.Point 50,15
$BuildingDescription.text = "Building"

# Grabs current settings from registry
$BuildingCurrent = New-Object Windows.Forms.Label
$BuildingCurrent.Location = New-Object Drawing.Point 70,77
$BuildingCurrent.Size = New-Object Drawing.Point 200,30
$BuildingCurrent.text = "Current building location is " + $CurrentBuildingKey

# Creates Button for setting building location
$Buildingbutton = New-Object Windows.Forms.Button
$Buildingbutton.text = "Set Building"
$Buildingbutton.Location = New-Object Drawing.Point 680,73

$BuildingButton.add_click(
{
if ($BuildingDropDown.text -eq "Building 1")
{
#Creates Combobox with size and location as well as array for the combobox and refreshes the form.
$form.controls.remove($LabDropDown)
[array]$LABDropDownArray= "N/A","Room 107","Room 101"
function Return-DropDown {
 $LABChoice=$LABDropDown.SelectedItem.ToString()
 $Form.Close()
 write-host $LABChoice
}
$LABDropDown=new-object System.Windows.Forms.Combobox
$LABDropDown.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
$LABDropDown.Location = New-Object Drawing.Point 445,105
$LABDropDown.Size = New-Object Drawing.Point 200,30
ForEach ( $Lab in $LABDropDownArray)
{
$LABDropDown.Items.Add($LAB)
}
$form.controls.add($LabDropDown)
$form.refresh()
}
if ($BuildingDropDown.text -eq "Building 2")
{
#Creates Combobox with size and location as well as array for the combobox and refreshes the form.
$form.controls.remove($LabDropDown)
[array]$LABDropDownArray= "N/A","Room 189","Room 159a"
function Return-DropDown {
 $LABChoice=$LABDropDown.SelectedItem.ToString()
 $Form.Close()
 write-host $LABChoice
}
$LABDropDown=new-object System.Windows.Forms.Combobox
$LABDropDown.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
$LABDropDown.Location = New-Object Drawing.Point 445,105
$LABDropDown.Size = New-Object Drawing.Point 200,30
ForEach ( $Lab in $LABDropDownArray)
{
$LABDropDown.Items.Add($LAB)
}
$form.controls.add($LabDropDown)
$form.refresh()
}
if ($BuildingDropDown.text -eq "Building 3")
{
#Creates Combobox with size and location as well as array for the combobox and refreshes the form.
$form.controls.remove($LabDropDown)
[array]$LABDropDownArray= "N/A","Room 120","Room 12"
function Return-DropDown {
 $LABChoice=$LABDropDown.SelectedItem.ToString()
 $Form.Close()
 write-host $LABChoice
}
$LABDropDown=new-object System.Windows.Forms.Combobox
$LABDropDown.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
$LABDropDown.Location = New-Object Drawing.Point 445,105
$LABDropDown.Size = New-Object Drawing.Point 200,30
ForEach ( $Lab in $LABDropDownArray)
{
$LABDropDown.Items.Add($LAB)
}
$form.controls.add($LabDropDown)
$form.refresh()
}
if ($BuildingDropDown.text -eq "Building 4")
{
#Creates Combobox with size and location as well as array for the combobox and refreshes the form.
$form.controls.remove($LabDropDown)
[array]$LABDropDownArray= "N/A","Room 23", "Room24"
function Return-DropDown {
 $LABChoice=$LABDropDown.SelectedItem.ToString()
 $Form.Close()
 write-host $LABChoice
}
$LABDropDown=new-object System.Windows.Forms.Combobox
$LABDropDown.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
$LABDropDown.Location = New-Object Drawing.Point 445,105
$LABDropDown.Size = New-Object Drawing.Point 200,30
ForEach ( $Lab in $LABDropDownArray)
{
$LABDropDown.Items.Add($LAB)
}
$form.controls.add($LabDropDown)
$form.refresh()
}
}
)

# Creates Lab for building row
$LABDescription = New-Object Windows.Forms.Label
$LABDescription.Location = New-Object Drawing.Point 385,107
$LABDescription.Size = New-Object Drawing.Point 50,15
$LABDescription.text = "Lab"

# Grabs current settings from registry
$LabCurrent = New-Object Windows.Forms.Label
$LabCurrent.Location = New-Object Drawing.Point 70,107
$LabCurrent.Size = New-Object Drawing.Point 200,30
$LabCurrent.text = "Current lab location is " + $CurrentLabKey

# Creates Combobox with size and location as well as array for the combobox
[array]$ROWDropDownArray= "N/A","Row A","Row B","Row C","Row D","Row E","Row F","Row G","Row H","Row I","Row J","Row K","Row L","Row M","Row N","Row O","Row P","Row Q","Row R","Row S","Row T","Row U","Row V","Row W","Row X","Row Y","Row Z"
function Return-DropDown {
 $LABChoice=$ROWDropDown.SelectedItem.ToString()
 $Form.Close()
 write-host $ROWChoice
}
$ROWDropDown = New-Object System.WIndows.Forms.Combobox
$ROWDropDown.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
$ROWDropDown.Location = New-Object Drawing.Point 445,135
$ROWDropDown.Size = New-Object Drawing.Point 200,30
ForEach ( $Row in $ROWDropDownArray)
{
$ROWDropDown.Items.Add($ROW)
}


# Creates Lab for building row
$ROWDescription = New-Object Windows.Forms.Label
$ROWDescription.Location = New-Object Drawing.Point 385,137
$ROWDescription.Size = New-Object Drawing.Point 50,15
$ROWDescription.text = "Row"

# Grabs current settings from registry
$ROWcurrent = New-Object Windows.Forms.Label
$ROWcurrent.Location = New-Object Drawing.Point 70,137
$ROWcurrent.Size = New-Object Drawing.Point 200,30
$ROWcurrent.text = "Current row location is " + $CurrentRowKey

# Creates Combobox with size and location as well as array for the combobox
[array]$RackDropDownArray= "N/A","Rack 1", "Rack 2", "Rack 3", "Rack 4", "Rack 5", "Rack 6", "Rack 7", "Rack 8", "Rack 9", "Rack 10", "Rack 11","Rack 12", "Rack 13", "Rack 14", "Rack 15", "Rack 16", "Rack 17", "Rack 18", "Rack 19", "Rack 20", "Rack 21", "Rack 22", "Rack 23", "Rack 24", "Rack 25", "Rack 26", "Rack 27", "Rack 28", "Rack 29", "Rack 30", "Rack 31", "Rack 32", "Rack 33", "Rack 34", "Rack 35", "Rack 36","Rack 37", "Rack 38", "Rack 39", "Rack 40", "Rack 41", "Rack 42", "Rack 43", "Rack 44", "Rack 45", "Rack 46", "Rack 47", "Rack 48", "Rack 49", "Rack 50", "Rack 51", "Rack 52", "Rack 53", "Rack 54", "Rack 55", "Rack 56", "Rack 57", "Rack 58", "Rack 59", "Rack 60", "Rack 61","Rack 62", "Rack 63", "Rack 64", "Rack 65", "Rack 66", "Rack 67", "Rack 68", "Rack 69", "Rack 70", "Rack 71", "Rack 72", "Rack 73", "Rack 74", "Rack 75", "Rack 76", "Rack 77", "Rack 78", "Rack 79", "Rack 80", "Rack 81", "Rack 82", "Rack 83", "Rack 84", "Rack 85", "Rack 86","Rack 87", "Rack 88", "Rack 89", "Rack 90", "Rack 91", "Rack 92", "Rack 93", "Rack 94", "Rack 95", "Rack 96", "Rack 97", "Rack 98", "Rack 99", "Rack 100"
function Return-DropDown {
 $RackChoice=$RackDropDown.SelectedItem.ToString()
 $Form.Close()
 write-host $RackChoice
}
$RackDropDown = New-Object System.Windows.Forms.Combobox
$RackDropDown.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
$RackDropDown.Location = New-Object Drawing.Point 445,165
$RackDropDown.Size = New-Object Drawing.Point 200,30
ForEach ( $Rack in $RackDropDownArray)
{
$RackDropDown.Items.Add($Rack)
}

# Creates Lab for building row
$RackDescription = New-Object Windows.Forms.Label
$RackDescription.Location = New-Object Drawing.Point 385,167
$RackDescription.Size = New-Object Drawing.Point 50,15
$RackDescription.text = "Rack"

# Grabs current settings from registry
$RackCurrent = New-Object Windows.Forms.Label
$RackCurrent.Location = New-Object Drawing.Point 70,167
$RackCurrent.Size = New-Object Drawing.Point 200,30
$RackCurrent.text = "Current rack location is " + $CurrentRackKey

# Creates Combobox with size and location as well as array for the combobox
[array]$RackUDropDownArray= "N/A","1U", "2U", "3U", "4U", "5U", "6U", "7U", "8U", "9U", "10U", "11U", "12U", "13U", "14U","15U", "16U", "17U", "18U", "19U", "20U", "21U", "22U", "23U", "24U", "25U", "26U", "27U", "28U", "29U", "30U","31U", "32U", "33U", "34U", "35U", "36U", "37U", "38U", "39U", "40U", "41U", "42U"
function Return-DropDown {
 $RackUChoice=$RackUDropDown.SelectedItem.ToString()
 $Form.Close()
 write-host $RackUChoice
}
$RackUDropDown = New-Object System.Windows.Forms.Combobox
$RackUDropDown.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
$RackUDropDown.Location = New-Object Drawing.Point 445,195
$RackUDropDown.Size = New-Object Drawing.Point 200,30
ForEach ( $RackU in $RackUDropDownArray)
{
$RackUDropDown.Items.Add($RackU)
}

# Creates Lab for building row
$RackUDescription = New-Object Windows.Forms.Label
$RackUDescription.Location = New-Object Drawing.Point 385,197
$RackUDescription.Size = New-Object Drawing.Point 50,15
$RackUDescription.text = "RackU"

# Grabs current settings from registry
$RackUCurrent = New-Object Windows.Forms.Label
$RackUCurrent.Location = New-Object Drawing.Point 70,197
$RackUCurrent.Size = New-Object Drawing.Point 200,30
$RackUCurrent.text = "Current rackU location is " + $CurrentRackUKey

# Create Button and set text and location
$button = New-Object Windows.Forms.Button
$button.text = "OK"
$button.Location = New-Object Drawing.Point 510,225

# Set up event handler to extract text from TextBox and display it on the Label.
$button.add_click(
{
#sets variable for $NewBuildingKey based on entered text, if no text is entered nothing is changed.
$NewBuildingKey=$BuildingDropDown.text
if ($NewBuildingKey)
{
Set-ItemProperty -Path HKLM:\Software\Microsoft\SomeCompany -Name Building -Value $NewBuildingKey
}
#sets variable for $NewLabKey based on entered text, if no text is entered nothing is changed.
$NewLabKey=$LABDropDown.text
if ($NewLabKey)
{
Set-ItemProperty -Path HKLM:\Software\Microsoft\SomeCompany -Name Lab -Value $NewLabKey
}
#sets variable for $NewRowKey based on entered text, if no text is entered nothing is changed.
$NewRowKey=$ROWDropDown.text
if ($NewRowKey)
{
Set-ItemProperty -Path HKLM:\Software\Microsoft\SomeCompany -Name Row -Value $NewRowKey
}
#sets variable for $NewRackKey based on entered text, if no text is entered nothing is changed.
$NewRackKey=$RackDropDown.text
if ($NewRackKey)
{
Set-ItemProperty -Path HKLM:\Software\Microsoft\SomeCompany -Name Rack -Value $NewRackKey
}
#sets variable for $NewRackUKey based on entered text, if no text is entered nothing is changed.
$NewRackUKey=$RackUDropDown.text
if ($NewRackUKey)
{
Set-ItemProperty -Path HKLM:\Software\Microsoft\SomeCompany -Name RackU -Value $NewRackUKey
}
#updates the label in the windows form to reflect the new data that has been entered by querying reg entries
$CurrentBuildingKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).Building
$CurrentLabKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).Lab
$CurrentRowKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).Row
$CurrentRackKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).Rack
$CurrentRackuKey=(get-itemproperty HKLM:\Software\Microsoft\SomeCompany).RackU
$BuildingCurrent.text = "Current building location is " + $CurrentBuildingKey
$LabCurrent.text = "Current lab location is " + $CurrentLabKey
$ROWcurrent.text = "Current row location is " + $CurrentRowKey
$RackCurrent.text = "Current rack location is " + $CurrentRackKey
$RackUCurrent.text = "Current rackU location is " + $CurrentRackUKey

}
)

# Add the controls to the Form
$form.controls.add($button)
$form.controls.add($label)
$form.controls.add($label2)
$form.controls.add($label3)
$form.controls.add($building)
$form.controls.add($lab)
$form.controls.add($row)
$form.controls.add($rack)
$form.controls.add($racku)
$form.controls.add($BuildingDescription)
$form.controls.add($LabDescription)
$form.controls.add($RowDescription)
$form.controls.add($RackDescription)
$form.controls.add($RackUDescription)
$form.controls.add($BuildingCurrent)
$form.controls.add($LabCurrent)
$form.controls.add($RowCurrent)
$form.controls.add($RackCurrent)
$form.controls.add($RackUCurrent)
$form.controls.add($BuildingDropDown)
$form.controls.add($LabDropDown)
$form.controls.add($RowDropDown)
$form.controls.add($RackDropDown)
$form.controls.add($RackUDropDown)
$form.controls.add($BuildingButton)

# Display the dialog
$form.ShowDialog()


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>