Powershell:当事件处理程序位于for循环内部时,复选框change event不会给出有效的结果

Powershell:当事件处理程序位于for循环内部时,复选框change event不会给出有效的结果,powershell,checkbox,event-handling,Powershell,Checkbox,Event Handling,我创建了一个复选框数组。我可以分别为每个复选框创建一个事件处理程序,但由于代码太长,我想如果我可以使用循环创建它们的话。当事件处理程序写入循环内部时,事件正在被处理,但显示的结果是错误的,即->当我选中第i个复选框时,事件已被处理,但$checkBox\u Charts[$i]。无论复选框是否选中,选中的始终返回False 编辑1: 我意识到,即使在任何复选框上引发checked事件,该代码也会返回for循环中最后一个元素的checked状态 添加完整的代码 代码: 在CheckStateC

我创建了一个复选框数组。我可以分别为每个复选框创建一个事件处理程序,但由于代码太长,我想如果我可以使用循环创建它们的话。当事件处理程序写入循环内部时,事件正在被处理,但显示的结果是错误的,即->当我选中第i个复选框时,事件已被处理,但
$checkBox\u Charts[$i]。无论复选框是否选中,选中的
始终返回
False

编辑1:

  • 我意识到,即使在任何复选框上引发checked事件,该代码也会返回for循环中最后一个元素的checked状态
  • 添加完整的代码
代码:


CheckStateChanged
事件的脚本块中,变量
$i
未知。要使其值在此处可用,必须将其存储在复选框可以读取的属性中。最好的地方是复选框自己的
标记
属性

在代码中,创建如下复选框:

$charts = "x","y","z"   # no need to surround this with @()

$checkBox_Charts = [System.Windows.Forms.checkbox[]]::new($charts.Count)
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
    # save the index $i in the Tag property of the checkbox itself
    $CheckBox.Tag = $i
    $CheckBox.Add_CheckStateChanged({
        # inside this scriptblock, the variable $i is unknown
        # so we use the index value stored in the Tag property earlier
        Write-Host "CP2: in Add_CheckStateChanged $($this.Checked)"
        Write-Host "CheckBox index: $($this.Tag)"
        Write-Host $checkBox_Charts[$this.Tag]
        Write-Host
    })
    $checkBox_Charts[$i] = $CheckBox
}
然后删除此处的代码

###########  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})

}
因为这一切都是在前面创建复选框时完成的(并且不会像您所注意到的那样工作)


旁注:

  • 更改此过时/不推荐的代码:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")


在控件的事件处理程序中,
$this
自动变量指控件本身

您能给我一些测试数据来填充吗?代码示例不完整,哪里定义了
$charts
图表?是图表defined@ArcSet我已经添加了完整的代码,请检查一下。 [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing

  • straighten the curly quotes you have in
    $Form.Text = ”My First Form with a working checkbox”
    to become
    $Form.Text = "My First Form with a working checkbox"
    .
    They won't hurt you in this case, but using curly quotes in code can lead to numerous weird problems. Never use them in code.