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.
Code Snippet:
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 = ”xxx”
# Creating array of Checkbox objects
$checkBox_Charts =[System.Windows.Forms.checkbox[]]::new(10)
$index_checkBox_Charts=0
# defining values to array of Checkbox objects
foreach ($key_chart in $charts.Keys){
$CheckBox = new-object System.Windows.Forms.checkbox
$CheckBox.Text = $key_chart
$checkBox_Charts[$index_checkBox_Charts] = $CheckBox
$index_checkBox_Charts++
}
# Create a group that will contain your checkbox buttons
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.text = "xxx"
# Add the check box array to GroupBox
$MyGroupBox.Controls.AddRange(@($checkBox_Charts))
# Add the GroupBox to Form
$Form.Controls.AddRange(@($MyGroupBox))
########### This is the important piece ##############
# #
# Do something when the state of the checkbox changes #
#######################################################
#Trying to create event handler for all check boxes in array in for loop than writing separately for checkbox
for($i=0; $i -lt 10; $i++){
$checkBox_Charts[$i].Add_CheckStateChanged({
$charts[$checkBox_Charts[$i]] =$checkBox_Charts[$i].Checked
Write-Host "CP2: in Add_CheckStateChanged " + $checkBox_Charts[$i].Checked
Write-Host $checkBox_Charts[$i]
})
}
# Activate the form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
}
#Call the function
checkbox_test
Aucun commentaire:
Enregistrer un commentaire