Windows 窗体 - 单击按钮将数据添加到列表视图 [英] Windows forms - add data to list view on button click
问题描述
我有一个 winform 应用程序,它在我点击 $button_UpdateTS
后填充一些数据,我如何添加存储在变量中的数据,在我点击该按钮后可用?
I have a winform application which populate some data after I click on $button_UpdateTS
, how do I add the data stored in a variable, that comes available after I click on that button ?
我想要的列表视图中的数据存储在一个名为 $results
The data I want in my list view is stored in an array called $results
$button_UpdateTS = New-Object System.Windows.Forms.Button
$button_UpdateTS.Location = New-Object System.Drawing.Size(15, 954)
$button_UpdateTS.Size = New-Object System.Drawing.Size(320, 32)
$button_UpdateTS.TextAlign = "MiddleCenter"
$button_UpdateTS.Text = "Update Tasksequence"
$button_UpdateTS.Add_Click( { $Results = Set-DynamicVariables
-Manufacturer "$($listview_Vendor.SelectedItems)"
-TSPackageID "$($ListView_Tasksequences.SelectedItems.SubItems[1].Text)" -WhatIf })
$Form.Controls.Add($button_UpdateTS)
这给了我:
$Results =
SKUNotExistsDriverName : XPS Notebook 9560
SKUNotExistsDriverID : PS10053F
SKUNotExistsDriverSKU : 07BE
SKUNotExistsDriverVersion : A12
SKUNotExistsBIOSName : XPS Notebook 9560
SKUNotExistsBIOSID : PS10053E
SKUNotExistsBIOSSKU : 07BE
SKUNotExistsBIOSVersion : 1.15.0
这是我希望它存储在的列表:
This is the list I want it stored in :
$Global:listview_NotExists_SKU = New-Object System.Windows.Forms.ListView
$listview_NotExists_SKU.Location = New-Object System.Drawing.Size(515, 670)
$listview_NotExists_SKU.Size = New-Object System.Drawing.Size(486, 235)
$listview_NotExists_SKU.View = "Details"
$listview_NotExists_SKU.FullRowSelect = $true
$listview_NotExists_SKU.MultiSelect = $true
$listview_NotExists_SKU.Sorting = "None"
$listview_NotExists_SKU.AllowColumnReorder = $true
$listview_NotExists_SKU.GridLines = $true
$listview_NotExists_SKU.Add_ColumnClick( { SortListView $this $_.Column })
$Form.Controls.Add($listview_NotExists_SKU)
我试过这个功能,但不起作用:
I tried with this function, but that does not work:
Function Get-Results {
ForEach ($Result in $Results) {
$listview_NotExists_SKU.Items.Add($Result)
}
}
$Form.Add_Shown( { $Form.Load; Get-results })
推荐答案
因为事件处理脚本块添加了例如.Add_Click()
在调用者的子作用域中运行,在那里分配给变量 $Results
($结果 = ...
) 创建一个范围局部变量,在其中设置事件处理程序的范围和随后调用的事件处理程序都无法看到该变量.
Because an event-handling script block added with e.g. .Add_Click()
runs in in a child scope of the caller, assigning to variable $Results
there ($Results = ...
) creates a scope-local variable that neither the scope in which the event handler was set up nor subsequently invoked event handlers can see.
要在 script 范围内创建一个变量,随后调用的事件处理程序也可以看到该变量[1],请使用 $script:
范围说明符:
To create a variable in the script scope, which subsequently invoked event handlers can see as well[1], use the $script:
scope specifier:
$button_UpdateTS.Add_Click( { $script:Results = ... } )
注意:如果设置事件处理程序的作用域不是 script 作用域(例如,如果代码在 函数 内)并且您想要要从事件处理程序中更一般地引用该范围,请使用 Set-Variable -Scope 1 -Name Results -Value ...
,它针对相应的 父 范围.[1]
Note: If the scope in which the event handlers are set up isn't the script scope (e.g., if the code is inside a function) and you want to more generically reference that scope from within an event handler, use Set-Variable -Scope 1 -Name Results -Value ...
, which targets the respective parent scope.[1]
[1] 有关 PowerShell 中作用域的更多信息,请参阅此答案的底部部分.
[1] For more information about scopes in PowerShell, see the bottom section of this answer.
这篇关于Windows 窗体 - 单击按钮将数据添加到列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!