I created an array of Check boxes. I could create a event handler for each check box separately but as it would be lengthy code I thought if I could create them using loop. When event handler is written inside loop the event is being handled but the result shown is wrong i.e -> when I select i'th check box the event is handled but $checkBox_Charts[$i].Checked is always returning False whether the box is checked or unchecked.
Edit 1:
- I realized even when checked event is raised on any check box this code is returning checked status of last element in for loop,
- Adding the complete code
Code:
function whichCharts(){
Write-Host "CP1: in whichCharts"
foreach ($key_chart in $charts.Keys){
Write-Host $charts[$key_chart]
}
}
function checkbox_test{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
# Set the size of your form
$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 600
$Form.Text = ”My First Form with a working checkbox”
# 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
$charts = @("x","y","z")
$checkBox_Charts =[System.Windows.Forms.checkbox[]]::new(3)
$index_checkBox_Charts=0
for ($i=0;$i -lt $charts.Count; $i++){
$CheckBox = new-object System.Windows.Forms.checkbox
$height = (60*$i)+20
$CheckBox.Location = new-object System.Drawing.Size(100,$height)
$CheckBox.Size = '150,50'
$CheckBox.Text = $charts[$i]
$CheckBox.Checked = $false
$checkBox_Charts[$i] = $CheckBox
}
# Add an OK button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '50,500'
$OKButton.Size = '100,40'
$OKButton.Text = "OK"
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
#Add a cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '255,100'
$CancelButton.Size = '100,40'
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
# Create a group that will contain your radio buttons
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '40,30'
$MyGroupBox.size = '800,400'
$MyGroupBox.text = "Do you like Cheese?"
# Add all the GroupBox controls on one line
$MyGroupBox.Controls.AddRange(@($checkBox_Charts))
$Form.Controls.AddRange(@($MyGroupBox,$OKButton,$CancelButton))
########### This is the important piece ##############
# #
# Do something when the state of the checkbox changes #
#######################################################
for($i=0; $i -lt 2; $i++){
$checkBox_Charts[$i].Add_CheckStateChanged({
Write-Host "CP2: in Add_CheckStateChanged " + $checkBox_Charts[$i].Checked
Write-Host $checkBox_Charts[$i]
Write-Host $i})
}
# Activate the form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
}
#Call the function
checkbox_test
$chartsdefined?