Switch statement produces incorrect result - winforms

I'm creating a script for our support department and interns. I have a working script but wanted to use a GUI to it a bit easier for our interns. At this moment I'm pulling my hair out right now, I've copied the function below and and would like to have the value of $task1_exec. But when I try to put the return $Result in a variable the $Task1 variable is printed not the $Task1_exec one.
$FormTitle = "Formtitle"
$ScriptPath = $(get-location).Path;
$task1 = "Task 1"
$task1_exec = "Perform task 1"
$task2 = "Task 2"
$task2_exec = "Perform task 2"
$task3 = "Task 3"
$task3_exec = "Perform task 3"
$task4 = "Task 4"
$task4_exec = "Perform task 4"
function Prompt-SelectionList
{
Param(
[Parameter(Position=0,Mandatory=$true)][string]$Description,
[Parameter(Position=1,Mandatory=$true)][string[]]$List
)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.Text = $FormTitle
$Form.Size = New-Object System.Drawing.Size(500,450) #(300,200)
$Form.StartPosition = "CenterScreen"
$btnOK = New-Object System.Windows.Forms.Button
$btnOK.Location = New-Object System.Drawing.Size(125,375)
$btnOK.Size = New-Object System.Drawing.Size(125,25)
$btnOK.Text = "Run..."
$Form.Controls.Add($btnOK)
$btnCancel = New-Object System.Windows.Forms.Button
$btnCancel.Location = New-Object System.Drawing.Size(250,375)
$btnCancel.Size = New-Object System.Drawing.Size(125,25)
$btnCancel.Text = "Exit"
$btnCancel.Add_Click({ $script:Result = $null; $Form.Close() })
$Form.Controls.Add($btnCancel)
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,20)
$Label.Size = New-Object System.Drawing.Size(480,20)
$Label.Text = $Description
$Form.Controls.Add($Label)
$ListBox = New-Object System.Windows.Forms.ListBox
$ListBox.Location = New-Object System.Drawing.Size(10,40)
$ListBox.Size = New-Object System.Drawing.Size(460,330)
$List | ForEach-Object { [void] $ListBox.Items.Add($_) }
$Form.Controls.Add($ListBox)
$Form.KeyPreview = $True
$Form.Add_KeyDown({ if ($_.KeyCode -eq "Enter") { $script:Result = $ListBox.SelectedItem; $Form.Close() }})
$Form.Add_KeyDown({ if ($_.KeyCode -eq "Escape") { $script:Result = $null; $Form.Close() }})
$btnOK.Add_Click({ $script:Result = $ListBox.SelectedItem; $Form.Close() })
$Form.Topmost = $True
$Form.Add_Shown({ $Form.Activate() })
[void] $Form.ShowDialog()
return $Result
}
function Do-PromptTask
{
switch -wildcard (Prompt-SelectionList "Chose task to run:" #($task1,$task2,$task3,$task4))
{
$task1{$task1_exec}
$task2{$task2_exec}
$Task3{$task3_exec}
$Task4{$task4_exec}
""{Exit} #End Selection (cancel selected)
# "*"{Do-PromptTask} #Keep prompting until "cancel" selected
}
}
Do-PromptTask
My wish is that I could return $task1_exec to a variable so when Do-PromptTask is finished the return can be used to call another script
. ".\Bin\$Result.ps1"

It looks like you're on the right track.
Let's pretend I run Do-PromptTask, chose Task 1, and click Run...
The $result variable from Prompt-SelectionList is passed back to the Switch statement and is out of scope at this point. The switch is doing its job and matching the conditional which then prints $task1_exec, but $result is of no use at this point. If you want to store $task1_exec as a variable, you'd need to set something equal to it. However, I will say you could just act on it in the switch statement without storing it in a variable, if that makes sense.
Less code approach...
function Do-PromptTask
{
switch -wildcard (Prompt-SelectionList "Chose task to run:" #($task1,$task2,$task3,$task4))
{
$task1{Invoke-Expression $task1_exec}
$task2{Invoke-Expression $task2_exec}
$Task3{Invoke-Expression $task3_exec}
$Task4{Invoke-Expression $task4_exec}
""{Exit} #End Selection (cancel selected)
# "*"{Do-PromptTask} #Keep prompting until "cancel" selected
}
}
If you're set on storing the variable, you could do something like this...
function Do-PromptTask
{
switch -wildcard (Prompt-SelectionList "Chose task to run:" #($task1,$task2,$task3,$task4))
{
$task1{$task_result = $task1_exec}
$task2{$task_result = $task2_exec}
$Task3{$task_result = $task3_exec}
$Task4{$task_result = $task4_exec}
""{Exit} #End Selection (cancel selected)
# "*"{Do-PromptTask} #Keep prompting until "cancel" selected
}
Invoke-Expression $task_result
}

Related

Struggling with an Until Loop combined with Get-Random and a Mouse Click in a PowerShell Form

I am struggling with an until loop in combination with a random picker and a mouse click in a PowerShell Form.
I am able to run a random picker without the form , where I have weekdays, picked by random and one by one day, is being removed, until the arraylist is empty. Works not bad.
$Weekdays = 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'
[System.Collections.ArrayList]$arraylist = $Weekdays
Write-Host $arraylist -ForegroundColor Green
pause
do {
$removetask = Get-Random $arraylist.ToArray()
$arraylist.Remove($removetask)
Write-Host $removetask
Write-Host $arraylist -ForegroundColor Red
pause
} until ($arraylist.Count -eq 0)
In another approach, I tried to do the same, but this time, I want to control the looping itself, that as soon as the first key from the arraylist is taken and shown in a label, I have to click the mouse button, so it continues to take the next random.
Without the do {} until () I got so far:
$TestForm = New-Object System.Windows.Forms.Form
$TestForm.Size = New-Object System.Drawing.Size (1200,800)
$TestForm.Text ='Random Test'
$TestForm.StartPosition = "CenterScreen"
$TestForm.AutoSize = $true
$TestForm.BringToFront()
$TestForm.BackgroundImageLayout = "Stretch"
[System.Collections.ArrayList]$Weekdays = 'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'
$TestLabel = New-Object System.Windows.Forms.label
$TestLabel.Location = New-Object System.Drawing.Size '500,200'
$TestLabel.Size = New-Object System.Drawing.Size '600,60'
$TestLabel.Font = New-Object System.Drawing.Font ('Times New Roman','20',[System.Drawing.FontStyle]::Bold)
$TestLabel.BackColor = 'Transparent'
$TestLabel.ForeColor = "Blue"
$removetask = Get-Random $Weekdays.ToArray()
$TestLabel.Text = $removetask
$TestForm.Controls.Add($TestLabel)
$TestButton = New-Object System.Windows.Forms.Button
$TestButton.Location = New-Object System.Drawing.Size '500,600'
$TestButton.Size = New-Object System.Drawing.Size '200,75'
$TestButton.Font = New-Object System.Drawing.Font ('Arial','10',[System.Drawing.FontStyle]::Bold)
$TestButton.Text = 'Next Random'
$TestForm.Controls.Add($TestButton)
$TestButton.Add_Click()
$TestForm.ShowDialog()
$TestForm.Dispose()
Now I have a few lines of code left, I am not able to include on such way, so it works on the following way.
The testform opens, and in a label I see chosen by random one of the weekdays. Clicking next will remove the chosen weekday from the arraylist and show the next weekday by random and will continue until the arraylist is empty.
The missing pieces to the puzzle are:
### The loop itself
do {} until ()
### code to find a Random value from $weekdays and write it into $removetask
$removetask = Get-Random $Weekdays.ToArray()
### code to remove the randomly chosen day and remove it from the arraylist
$Weekdays.Remove($removetask)
#### check if array is empty
($weekdays.Count -eq 0)
I was playing around with the codes and tried with Button.Add_Click() and also with ButtonClickEvent {} but either, the loop is not running, the counter is not working correctly or I somehow messed up the code on such way, that it is stuck somewhere, that not even the form is being shown.
The following enhanced adjustment of your script implements some kind of a loop in the form.
Note that no loop keywords (like do, while, until) and even no if keyword are used:
### Load Assemblies for creating form & controls ###
if ( -not ("System.Windows.Forms.Form" -as [type]) ) {
Add-Type -AssemblyName System.Windows.Forms
}
if ( -not ("System.Drawing.Font" -as [type]) ) {
Add-Type -AssemblyName System.Drawing
}
$TestForm = New-Object System.Windows.Forms.Form
$TestForm.Size = New-Object System.Drawing.Size (1200,800)
$TestForm.Text ='Random Test'
$TestForm.StartPosition = "CenterScreen"
$TestForm.AutoSize = $true
$TestForm.BringToFront()
$TestForm.BackgroundImageLayout = "Stretch"
$TestLabel = New-Object System.Windows.Forms.label
$TestLabel.Location = New-Object System.Drawing.Size '500,200'
$TestLabel.Size = New-Object System.Drawing.Size '600,60'
$TestLabel.Font = New-Object System.Drawing.Font ('Times New Roman','20',[System.Drawing.FontStyle]::Bold)
$TestLabel.BackColor = 'Transparent'
$TestLabel.ForeColor = "Blue"
$TestForm.Controls.Add($TestLabel)
$TestLabe2 = New-Object System.Windows.Forms.Label
$TestLabe2.Location = New-Object System.Drawing.Size '200,300'
$TestLabe2.Size = New-Object System.Drawing.Size '900,200'
$TestLabe2.Font = New-Object System.Drawing.Font ([System.Windows.Forms.Label]::DefaultFont.Name,'16',[System.Drawing.FontStyle]::Italic)
$TestLabe2.BackColor = 'Transparent'
$TestLabe2.ForeColor = [System.Drawing.Color]::MidnightBlue
$TestForm.Controls.Add($TestLabe2)
$TestButton = New-Object System.Windows.Forms.Button
$TestButton.Location = New-Object System.Drawing.Size '500,600'
$TestButton.Size = New-Object System.Drawing.Size '200,75'
$TestButton.Font = New-Object System.Drawing.Font ('Arial','10',[System.Drawing.FontStyle]::Bold)
$TestButton.Text = 'Next Random'
$TestForm.Controls.Add($TestButton)
$TestButtoX = New-Object System.Windows.Forms.Button
$TestButtoX.Location = New-Object System.Drawing.Size '200,600'
$TestButtoX.Size = New-Object System.Drawing.Size '200,75'
$TestButtoX.Font = New-Object System.Drawing.Font ('Arial','10',[System.Drawing.FontStyle]::Bold)
$TestButtoX.Text = 'Next Round'
$TestButtoX.Enabled = $false
$TestForm.Controls.Add($TestButtoX)
Function Swap-Buttons {
$TestButton.Enabled = [bool]$script:Weekdays.Count
$TestButtoX.Enabled = -not [bool]$script:Weekdays.Count
}
Function RemoveWeekday {
$script:removetask = Get-Random $script:Weekdays.ToArray()
$script:Weekdays.Remove($script:removetask)
$TestLabe2.Text = ('(remain {0})' -f $script:Weekdays.Count), ($script:Weekdays -join ', ') -join ': '
$TestLabel.Text = $script:removetask
Swap-Buttons
}
Function DefineWeek {
$script:Weekdays = [System.Collections.ArrayList]([System.Enum]::GetNames([System.DayOfWeek]))
<#
# debugging: try another array list (a larger one)
$script:Weekdays = [System.Collections.ArrayList]([System.Drawing.Color] |
Get-Member -MemberType Properties -Static -Force |
Where-Object Name -match ".+blue" |
Select-Object -ExpandProperty Name
)
<##>
}
$TestButton.Add_Click({
RemoveWeekday
})
$TestButtoX.Add_Click({
DefineWeek
$TestButtoX.Enabled = $false
$TestButton.Enabled = $true
RemoveWeekday
})
$script:removetask = ''
DefineWeek
RemoveWeekday
$TestForm.ShowDialog()
$TestForm.Dispose()

Manual Checkbox enable

I have a ComboBox control on my form. What I want is when I change an item in the ComboBox from one to another, the event was handled. It is important that when changing and not when choosing the same element. All this time I used ComboBox.Add_SelectionChangeCommitted($function), but soon I realized that the block that is also executed by the handler when the same (selected) item is selected from the list. A little digging in ComboBox events I am completely confused. Having tried several events (SelectedItemChanged, SelectedIndexChanged) I could never able to achieve the desired result.
An example of what I want to do and what should not be done several times when choosing the same element. When the block of code for Manual is executed several times, the contents of all TextBox are cleared, but I don’t want to.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '420,240'
$Form.TopMost = $false
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false
$ComboBox = New-Object system.Windows.Forms.ComboBox
$ComboBox.width = 391
$ComboBox.height = 47
#('Automatic (DHCP)','Manual Input') | ForEach-Object {[void] $ComboBox.Items.Add($_)}
$ComboBox.location = New-Object System.Drawing.Point(13,57)
$ComboBox.DropDownStyle = 'DropDownList'
$ComboBox.DrawMode = 'OwnerDrawFixed'
$Button = New-Object system.Windows.Forms.Button
$Button.text = "Set"
$Button.width = 60
$Button.height = 30
$Button.visible = $false
$Button.enabled = $false
$Button.location = New-Object System.Drawing.Point(336,126)
$CheckBox = New-Object system.Windows.Forms.CheckBox
$CheckBox.width = 65
$CheckBox.height = 15
$CheckBox.visible = $false
$CheckBox.Checked = $false
$CheckBox.enabled = $true
$CheckBox.location = New-Object System.Drawing.Point(16,210)
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.Name = "TextBox1"
$TextBox1.multiline = $false
$TextBox1.width = 40
$TextBox1.height = 20
$TextBox1.visible = $false
$TextBox1.enabled = $true
$TextBox1.location = New-Object System.Drawing.Point(81,100)
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.Name = "TextBox2"
$TextBox2.multiline = $false
$TextBox2.width = 40
$TextBox2.height = 20
$TextBox2.visible = $false
$TextBox2.enabled = $false
$TextBox2.location = New-Object System.Drawing.Point(81,208)
$Form.controls.AddRange(#($TextBox1,$TextBox2,$Button,$ComboBox,$CheckBox))
$global:ManualChecked = $null
$global:AutomaticChecked = $null
$ComboBox.Add_SelectionChangeCommitted($methodSelection)
$netwValues = New-Object 'System.Collections.Hashtable'
$methodSelection =
{
switch($ComboBox.Text)
{
"Manual Input"
{
$CheckBox.Visible = $Button.Visible = $true
$Button.Enabled = $false
ForEach ($control in $Form.controls)
{
if ($control -is [System.Windows.Forms.TextBox] )
{
$control.Visible = $control.Enabled = $true
$control.Clear()
if($netwValues.Count -gt 0)
{
$control.Text = $netwValues.Item($control.Name)
$netwValues.Remove($control.Name)
}
}
}
if($global:ManualChecked -eq 1)
{
$CheckBox.Checked = $true
$TextBox2.Enabled = $true
}
else
{
$CheckBox.Checked = $false
$TextBox2.Enabled = $false
}
}
"Automatic (DHCP)"
{
ForEach($control in $Form.controls)
{
if($control -is [System.Windows.Forms.TextBox] -or $control -is [System.Windows.Forms.CheckBox] -or $control -is [System.Windows.Forms.Button])
{
$control.Visible = $control.Enabled = $true
if($control -is [System.Windows.Forms.Label] -or $control -is [System.Windows.Forms.TextBox])
{
$control.Enabled = $false
if($control -is [System.Windows.Forms.TextBox])
{
if($control.Text.Length)
{
$netwValues.Add($control.Name,$control.Text)
$control.Clear()
}
}
}
}
}
if($global:AutomaticChecked -eq 1)
{
$CheckBox.Checked = $true
}
else
{
$CheckBox.Checked = $false
}
}
}
}
You can implement this logic yourself.
# Simple class for using as item for combobox instead of just string.
class CbItem
{
CbItem([string] $value)
{
$this.Value = $value
}
[string] $Value
[string] ToString()
{
return $this.Value
}
}
# Add instances of this class into combobox.
$ComboBox.Items.Add([CbItem]::new('Automatic (DHCP)'))
$ComboBox.Items.Add([CbItem]::new('Manual Input'))
# Create a variable for storing current combobox's item.
$global:CurrentCbItem = $null
# Add logic at the begining of handler for exiting if selected item is the same as the last one.
$methodSelection =
{
if ([Object]::ReferenceEquals($global:CurrentCbItem, $ComboBox.SelectedItem))
{
return
}
$global:CurrentCbItem = $ComboBox.SelectedItem
...
}

Using a function to populate a textbox in Powershell

I currently have a script that queries our AD for the users' AD attribute "Department".
Right now, the script will run "successfully" but all output for Textbox2 goes to the console instead of TextBox2.
If I change my function Get-CCUsers to be a query without variables, like Get-ADUser -Filter "Department -eq 19330" (we use numbers for our departments) then the output shows in TextBox2 as I want it to.
How can I get my function to populate TextBox2?
BTW, this script was cobbled together with my limited understanding, and there may well be superfluous or nonsense lines in here.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Howard Center Profile Migration'
$form.Size = New-Object System.Drawing.Size(800,650)
$form.StartPosition = 'CenterScreen'
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$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)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the Cost Center # in the space below:'
$form.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(100,20)
$form.Controls.Add($textBox)
$RunButton = New-Object System.Windows.Forms.Button
$RunButton.Location = New-Object System.Drawing.Point(75,120)
$RunButton.Size = New-Object System.Drawing.Size(75,23)
$RunButton.Text = 'RUN'
#$RunButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
<#$RunButton.Add_Click({
#add here code triggered by the event
$TextBox2.Text = Get-Process | Format-Table -Property ProcessName, Id, CPU -AutoSize | Out-String
})
#>
$form.AcceptButton = $RunButton
$form.Controls.Add($RunButton)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,70)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'When you are ready click the Run button below'
$form.Controls.Add($label2)
$TextBox2 = New-Object system.windows.Forms.TextBox
$TextBox2.Text = ""
$TextBox2.Multiline = $true
$TextBox2.BackColor = "#013686"
$TextBox2.ScrollBars = "Both"
$TextBox2.Width = 750
$TextBox2.Height = 450
$TextBox2.location = new-object system.drawing.point(10,150)
$TextBox2.Font = "Microsoft Sans Serif,10"
$TextBox2.ForeColor = "#ffffff"
$Form.controls.Add($TextBox2)
$form.Topmost = $true
function Get-CCUsers {
Write-Host "The textbox text is $textbox.Text"
$dept = $textBox.Text
$deptUsers = Get-ADUser -Filter "Department -eq $dept"
ForEach ($user in $deptUsers) {
IF ( ((get-aduser $user).enabled) -eq $True ) {
$UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
$UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
Write-Host "$user, $UHomeDir, $UProfPath"
}
}
}
$RunButton.Add_Click({
$TextBox2.Text = Get-CCUsers
})
$TextBox2.Add_TextChanged({
$TextBox2.SelectionStart = $TextBox2.Text.Length
$TextBox2.ScrollToCaret()
})
$form.Add_Shown({$Form.Update()})
$result = $form.ShowDialog()
$global:x = $textBox.Text
# $x
# if ($result -eq [System.Windows.Forms.DialogResult]::OK)
#{
#}
I have made your script working changing the 'Get-CCUSers' function
function Get-CCUsers {
Write-Host "The textbox text is $textbox.Text"
$dept = $textBox.Text
$deptUsers = Get-ADUser -Filter "Department -eq '$dept'"
$res = #()
ForEach ($user in $deptUsers) {
IF ( ((get-aduser $user).enabled) -eq $True ) {
$UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
$UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
$res += "$user, $UHomeDir, $UProfPath"
}
}
return $res
}
In short:
I removed the Write-Host (the output was actually redirected to stdout)
I added a $res in the function initialized as array
In the ForEach loop, results are added as items in the array
The $res is returned by the function, and then used $RunButton

Build and respond to events on multiple check boxes

I am attempting to make a form with multiple check boxes, based on the array passed to the form creation function. I can calculate the correct location based on the count of what checkbox I am at is, but I am having trouble (I think) dealing with events. I have this for now (partial code, obviously)
$checkboxCount = 1
foreach ($year in $years) {
$checkbox = new-object System.Windows.Forms.checkbox
$checkbox.Size = new-object System.Drawing.Size(100,20)
$checkbox.Location = new-object System.Drawing.Size(10,($checkbox.Size.Height*$checkboxCount-10))
$checkbox.Text = "Revit $year"
$checkbox.Checked = $true
$Form.Controls.Add($checkbox)
$checkbox.Add_CheckStateChanged({
$results.$year = $checkbox.Checked
})
$checkboxCount ++
}
and the check boxes are created correctly, but when I return $results from the function they are all True. I am basing the code off of this, which works but with a static number of check boxes.
function checkbox_test{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$results = #{
one = $true
two = $true
}
$optionCount = 2
# Set the size of your form
$Form = New-Object System.Windows.Forms.Form
$Form | Format-List *
$form.FormBorderStyle = 'FixedDialog'
$form.ShowInTaskbar = $false
$Form.width = 300
$Form.height = 150
$Form.Text = ”Px Tools Updater”
# Set the font of the text to be used within the form
$Font = New-Object System.Drawing.Font("Times New Roman",12)
$Form.Font = $Font
# create your checkbox
$checkbox1 = new-object System.Windows.Forms.checkbox
$checkbox1.Location = new-object System.Drawing.Size(10,7)
$checkbox1.Size = new-object System.Drawing.Size(100,20)
$checkbox1.Text = "One"
$checkbox1.Checked = $true
$Form.Controls.Add($checkbox1)
# create your checkbox
$checkbox2 = new-object System.Windows.Forms.checkbox
$checkbox2.Location = new-object System.Drawing.Size(10,27)
$checkbox2.Size = new-object System.Drawing.Size(100,20)
$checkbox2.Text = "Two"
$checkbox2.Checked = $true
$Form.Controls.Add($checkbox2)
# Add an OK button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(10,70)
$OKButton.Size = new-object System.Drawing.Size(60,30)
$OKButton.Text = "OK"
$OKButton.Add_Click({$Form.Close()})
$form.Controls.Add($OKButton)
#Add a cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = new-object System.Drawing.Size(225,100)
$CancelButton.Size = new-object System.Drawing.Size(60,30)
$CancelButton.Text = "Cancel"
$CancelButton.Margin = 0
$CancelButton.Add_Click({$Form.Close()})
$form.Controls.Add($CancelButton)
$checkbox1.Add_CheckStateChanged({
$results.one = $checkbox1.Checked
})
$checkbox2.Add_CheckStateChanged({
$results.two = $checkbox2.Checked
})
# Activate the form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
$results
}
I am not sure if I am going wrong with the way I am referencing the results hash table, or maybe the entire approach is wrong?
Edit: I had a thought, that $year is meaningless in the event handler, so I added
$checkbox.Name = $year
and revised the event handler to
$results.($checkbox.Name) = $checkbox.Checked
and
$results.($Self.Name) = $checkbox.Checked
But no joy with either. But what is weird is that using $self results in an odd extra key being added to $return. It has no key name, but the value matches the last change made to any checkbox.
EDIT #2: In further testing, I changed the handler to
$results.2019 = $checkbox.Checked
expecting that to mean any change results in that change applied to the 2019 key. not so. So I am thinking it relates to the way hash tables are passed and referenced and likely I am doing this all wrong. Perhaps worrisome is the fact that I can find tons of information on making check boxes react to and change other parts of the form, but so far nothing on just getting results back.
EDIT #3: OK, seems the answer (of sorts) is there is no need for event handlers, because I only really care about end state anyway. So, with some extra cleanup to also handle Cancel I have this, and it works. Still curious how, or if, I could interact directly with $results from an event handler though.
function checkbox_test{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$years = #('2016', '2017', '2018', '2019')
$optionCount = $years.Count
# Set the size of your form
$Form = New-Object System.Windows.Forms.Form
$form.FormBorderStyle = 'FixedDialog'
$form.ShowInTaskbar = $false
$Form.width = 300
$Form.height = ($years.Count * 30 + 50 + 40) #150
$Form.Text = ”Px Tools Updater”
# Set the font of the text to be used within the form
$Font = New-Object System.Drawing.Font("Times New Roman",12)
$Form.Font = $Font
# create Checkboxes
$checkboxCount = 1
foreach ($year in $years) {
$checkbox = new-object System.Windows.Forms.checkbox
$checkbox.Size = new-object System.Drawing.Size(100,20)
$checkbox.Location = new-object System.Drawing.Size(10,($checkbox.Size.Height*$checkboxCount-10))
$checkbox.Text = "Revit $year"
$checkbox.Name = $year
$checkbox.Checked = $true
$Form.Controls.Add($checkbox)
$checkboxCount ++
}
# Add an OK button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Size = new-object System.Drawing.Size(60,30)
$OKButton.Location = new-object System.Drawing.Size(10,($form.DisplayRectangle.Height - $OKButton.Size.Height - 10))
$OKButton.Text = "OK"
$OKButton.Add_Click({
$Form.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Form.Close()
})
$form.Controls.Add($OKButton)
#Add a cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Size = new-object System.Drawing.Size(60,30)
$CancelButton.Location = new-object System.Drawing.Size(($form.DisplayRectangle.Width - $CancelButton.Size.Width - 10),($form.DisplayRectangle.Height - $CancelButton.Size.Height - 10))
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({
$Form.Close()
})
$form.Controls.Add($CancelButton)
# Activate the form
$Form.Add_Shown({$Form.Activate()})
if ($Form.ShowDialog() -eq 'OK') {
$results = New-Object Collections.Specialized.OrderedDictionary
foreach ($control in $form.Controls) {
if ($years -contains $control.Name) {
$results.Add($control.Name, $control.Checked)
}
}
} else {
$results = $null
}
[void] $Form.Dispose
$results
}
#Call the function
$returned = checkbox_test
Foreach ($key in $returned.keys) {
Write-Host "[$key] $($returned.$key)!"
}
The foreach loop used to assign each checkbox event handler above effectively overwrites the previous one, and therefore you only capture the last one. (2019)
Instead, assign the event hander the way it's traditionally done in Windows Forms:
$results = New-Object Collections.Specialized.OrderedDictionary;
foreach ($year in $years) {
$checkbox = new-object System.Windows.Forms.checkbox
$Form.Controls.Add($checkbox);
$checkbox.Size = new-object System.Drawing.Size(100,20)
$checkbox.Location = new-object System.Drawing.Size(10,($checkbox.Size.Height*$checkboxCount-10))
$checkbox.Text = "Revit $year"
$checkbox.Name = $year
$checkbox.Checked = $true
$results.Add($year, $checkbox.Checked);
# HERE!
$checkbox.Add_CheckStateChanged({
# $this -eq sender, optionally $_ -eq EventArgs
$year = $this.name;
$results.$year = $this.Checked;
});
$checkboxCount++
}

PowerShell Calendar Popup

I am trying to use the PowerShell script from http://technet.microsoft.com/en-us/library/ff730942.aspx
After choosing a date and hitting enter, the variable $dtmDate has no data. Please help. Is it possible to add an OK/Cancel button?
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object Windows.Forms.Form
$objForm.Text = "Select a Date"
$objForm.Size = New-Object Drawing.Size #(243,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Enter")
{
$dtmDate=$objCalendar.SelectionStart
$objForm.Close()
}
})
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Escape")
{
$objForm.Close()
}
})
$objCalendar = New-Object System.Windows.Forms.MonthCalendar
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
if ($dtmDate)
{
Write-Host "Date selected: $dtmDate"
}
$dtmDate has no value in the context of your if statement because that code isn't being executed on your enter keypress... the block in the context of Add_KeyDown is. Try executing something like this instead:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$dtmDate = $null
# I've moved your Date printing logic into this function that can be called elsewhere.
Function Do-Work
{
if ($dtmDate -ne $null)
{
Write-Host "Date selected: $dtmDate"
}
}
$objForm = New-Object Windows.Forms.Form
$objForm.Text = "Select a Date"
$objForm.Size = New-Object Drawing.Size #(243,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Enter")
{
$dtmDate = $objCalendar.SelectionStart
$objForm.Close()
Do-Work
}
})
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Escape")
{
$objForm.Close()
}
})
$objCalendar = New-Object System.Windows.Forms.MonthCalendar
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
The new function Do-Work is defined outside of the scope of Add_KeyDown, but since we call it there it will get executed on your Enter keypresses.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
New-Variable dtmDate -Option AllScope
$dtmDate = $null
$objForm = New-Object Windows.Forms.Form
$objForm.Text = "Select a Date"
$objForm.Size = New-Object Drawing.Size #(190,190)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown(
{
if ($_.KeyCode -eq "Enter")
{
$dtmDate=$objCalendar.SelectionStart
$objForm.Close()
}
})
$objForm.Add_KeyDown(
{
if ($_.KeyCode -eq "Escape") {$objForm.Close()}
})
$objCalendar = New-Object System.Windows.Forms.MonthCalendar
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
if ($dtmDate) {Write-Host "Date selected: $dtmDate" -ForegroundColor Green}
The line "New-Variable dtmDate -Option AllScope" allows the variable to be brought outside of the form scope, so you can use it further in your script.
I left out the creation of a function for the write host. If you want to do it the proper way, create a function of the entire script block.
The above script will work in the way you want it to. It's working for me!
You have to add:
New-Variable dtmDate -Option AllScope

Resources