Last active
December 28, 2017 10:23
-
-
Save CosmosKey/7d5a4e2207d41b1e7be326b3c321a2e8 to your computer and use it in GitHub Desktop.
SelectBuildingAndOfficeForm.ps1 A piece of DropDown Forms sample code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DropDown Data | |
$office = @{ | |
'HQ' = 'Ground floor','1st floor','2nd floor','3rd floor' | |
'Garage' = 'Shop floor','Office' | |
'Sales office' = 'Ground floor','1st floor' | |
} | |
# Load assemblies | |
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | |
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | |
$script:LocationWidth = 10 | |
$script:LocationHeight = 10 | |
$script:DefaultControlWidth = 260 | |
$script:DefaultControlHeight = 25 | |
$form = New-Object System.Windows.Forms.Form | |
$form.width = 300 | |
$form.height = 200 | |
$form.Text = "Floor selector." | |
# Functions to get the values from a growing stack of controls | |
Function Get-NextControlLocation { | |
New-Object -TypeName System.Drawing.Size -ArgumentList $script:LocationWidth,$script:LocationHeight | |
$script:LocationWidth = $script:LocationWidth | |
$script:LocationHeight = $script:LocationHeight+30 | |
} | |
Function Get-DefaultControlSize { | |
New-Object -TypeName System.Drawing.Size -ArgumentList $script:DefaultControlWidth,$script:DefaultControlHeight | |
} | |
# Define the controls | |
$label = new-object System.Windows.Forms.Label | |
$label.Location = Get-NextControlLocation | |
$label.size = Get-DefaultControlSize | |
$label.Text = "Select the office you are working in." | |
$form.Controls.Add($label) | |
$dropDownBuilding = New-Object System.Windows.Forms.ComboBox | |
$dropDownBuilding.Location = Get-NextControlLocation | |
$dropDownBuilding.Size = Get-DefaultControlSize | |
$dropDownBuilding.Items.AddRange($office.Keys) | |
$form.Controls.Add($dropDownBuilding) | |
$dropDownOffice = New-Object System.Windows.Forms.ComboBox | |
$dropDownOffice.Location = Get-NextControlLocation | |
$dropDownOffice.Size = Get-DefaultControlSize | |
$form.Controls.Add($dropDownOffice) | |
$dropDownOffice.Enabled = $false | |
$buttonOK = New-Object System.Windows.Forms.Button | |
$buttonOK.Location = Get-NextControlLocation | |
$buttonOK.Size = Get-DefaultControlSize | |
$buttonOK.Text = "OK" | |
$form.Controls.Add($buttonOK) | |
$buttonCancel = New-Object System.Windows.Forms.Button | |
$buttonCancel.Location = Get-NextControlLocation | |
$buttonCancel.Size = Get-DefaultControlSize | |
$buttonCancel.Text = "Cancel" | |
$form.Controls.Add($buttonCancel) | |
# Define events | |
$clicked_Cancel = { | |
$form.Close() | |
} | |
$clicked_OK = { | |
$selectedBuilding = $dropDownBuilding.SelectedItem | |
$selectedOffice = $dropDownOffice.SelectedItem | |
if($selectedBuilding -eq $null -or $selectedOffice -eq $null) { | |
[System.Windows.Forms.MessageBox]::Show("Please select both building and floor.") | |
} else { | |
[System.Windows.Forms.MessageBox]::Show(("Selected building {0}, selected office {1}" -f $selectedBuilding,$selectedOffice )) | |
$form.Close() | |
} | |
} | |
$selected_Building = { | |
$dropDownOffice.Enabled = $true | |
$dropDownOffice.Items.Clear() | |
$dropDownOffice.Items.AddRange($office[$dropDownBuilding.SelectedItem]) | |
} | |
# Wire the events up | |
$dropDownBuilding.Add_SelectedValueChanged($selected_Building) | |
$buttonOK.Add_Click($clicked_OK) | |
$buttonCancel.Add_Click($clicked_Cancel) | |
# Show and activate the form | |
$form.Add_Shown({$form.Activate()}) | |
$form.ShowDialog() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment