winforms - Winform应用程序在某些变量包含数据后将数据添加到列表视图
问题描述
是否可以listview
在某个变量包含数据后向 a 添加数据?
我有一个function
,当我按下按钮时,它会从 ConfigMgr 获取信息。
在我按下该按钮后,一些信息将存储在一个名为$Results
. 我希望winform listview
等到变量$Results
包含数据,然后加载 my function
,这会将数据添加到 my listview
- 这可能吗?
如果我不清除变量$Results
并再次运行winform,它工作正常,因为在我的脚本中变量$Results
不为空$Form.Add_Shown( { $Form.Activate(); Results })
有没有等效的方法来实现我想要的?
这是我的功能:
Function Get-SKUNotExists {
#Get objects stored in $Results
$listview_NotExists_SKU_Results = $Results | Select-Object Name,ID
# Compile a list of the properties
$listview_NotExists_SKU_Properties = $listview_NotExists_SKU_Results[0].psObject.Properties
# Create a column in the listView for each property
$listview_NotExists_SKU_Properties | ForEach-Object {
$listview_NotExists_SKU.Columns.Add("$($_.Name)")
}
# Looping through each object in the array, and add a row for each
ForEach ($listview_NotExists_SKU_Result in $listview_NotExists_SKU_Results) {
# Create a listViewItem, and assign it it's first value
$listview_NotExists_SKU_Item = New-Object System.Windows.Forms.ListViewItem($listview_NotExists_SKU_Result.Name)
# For each properties, except for 'Id' that we've already used to create the ListViewItem,
# find the column name, and extract the data for that property on the current object/Tasksequence
$listview_NotExists_SKU_Result.psObject.Properties | Where-Object { $_.Name -ne "ID" } | ForEach-Object {
$listview_NotExists_SKU_Item.SubItems.Add("$($listview_NotExists_SKU_Result.$($_.Name))")
}
# Add the created listViewItem to the ListView control
# (not adding 'Out-Null' at the end of the line will result in numbers outputred to the console)
$listview_NotExists_SKU.items.Add($listview_NotExists_SKU_Item)
}
# Resize all columns of the listView to fit their contents
$listview_NotExists_SKU.AutoResizeColumns("HeaderSize")
}
这是生成数据的按钮$results:
# Adding another button control to Form
$button_whatif = New-Object System.Windows.Forms.Button
$button_whatif.Location = New-Object System.Drawing.Size(352, 954)
$button_whatif.Size = New-Object System.Drawing.Size(320, 32)
$button_whatif.TextAlign = "MiddleCenter"
$button_whatif.Text = “WhatIf”
$button_whatif.Add_Click( { $script:Results = Set-DynamicVariables -Manufacturer "$($listview_Vendor.SelectedItems)" -TSPackageID "$($ListView_Tasksequences.SelectedItems.SubItems[1].Text)" -WhatIf })
$Form.Controls.Add($button_whatif)
解决方案
Get-SKUNotExists
分配给后直接调用您的列表填充函数( ) $Results
:
$button_whatif.Add_Click({
$Results = Set-DynamicVariables -Manufacturer "$($listview_Vendor.SelectedItems)" -TSPackageID "$($ListView_Tasksequences.SelectedItems.SubItems[1].Text)" -WhatIf
Get-SKUNotExists
})
请注意,我已经$scipt:
从 中删除了范围说明符$Results
,因为不再需要它,因为您正在Get-SKUNotExists
从同一个范围调用,这意味着在其中运行的子范围Get-SKUNotExists
隐式地看到它。
那说:
一般来说,如果一个值是直接可用的,那么将它作为参数(参数)传递而不是依赖于 PowerShell 的动态作用域(后代作用域隐式地从祖先作用域中看到变量,除非被局部变量遮蔽)会更加健壮。
如果必须在多个事件处理程序之间共享值,则将它们存储在脚本范围 (
$script:...
) 中,或者更一般地说,可能需要父范围,毕竟 - 请参阅此答案。
推荐阅读
- c# - 如何从 C# 中支持 ONVIF 的 VMS 和记录器获取 RTSP url
- python - Python:如何处理子类中的类型提示?
- c++ - 无法在#include 的 Visual Studio 中链接“GLFW”
- x86 - 如果汇编代码跳转到包含错误指令的地址会发生什么?
- embedded - esp32 中的以下错误是什么意思?
- ssas - SSAS:使用当前用户对 SQL 数据库进行 DirectQuery
- node.js - 将 Node.js API 连接到 Microsoft Azure / Azure CLI 上的虚拟机
- google-cloud-platform - 为什么 smtp 的 wordpress 给我不同的错误?
- api - Instagram 使用哪个 API 调用来获取主页帖子提要?
- ruby-on-rails - 使用连接的太阳黑子搜索关联