首页 > 解决方案 > 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)

标签: winformspowershelllistview

解决方案


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:...) 中,或者更一般地说,可能需要范围,毕竟 - 请参阅此答案


推荐阅读