简体   繁体   中英

Display WinForms object properties in Out-GridView

While testing I frequently want to check the properties of a WinForms object by piping the variable to Out-GridView, but it's not displaying properly. Each property is showing up as a separate column. I was able to sort of get what I'm looking for by using a calculated property, but I have to reference the object by name within the calculation. It also feels like maybe I'm making this harder than it needs to be. I'm pretty new with Powershell so please forgive my ineptitude.

Here's what I have at the moment:

Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form | Get-Member -MemberType Property | Select-Object Name, @{Name='Value';Expression={$Form.($_.Name)}} | Out-GridView

It sounds like you want each property of the form to be shown on its own row in the grid view, instead of showing all properties in the columns of a single row, which happens by default. To put it differently, you're looking for Format-List -like display instead of Format-Table -like display.

A simple solution is to use the intrinsic psobject property to reflect on an object's properties:

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form

$form.psobject.Properties | 
  Select-Object Name, Value |
  Out-GridView

Sample output - note how each property name-value pair is on its own row:

out-gridview 屏幕截图

function Show-ObjectProperties([Parameter(ValueFromPipeline)]$F){
    ($F|Get-Member -M Property).Name|ForEach{@{$_=$F.$_}}|
    OGV -Title ($MyInvocation.Line -replace '^\$|\s*\|.+')
}


$MainForm = [Windows.Forms.Form]::new()
$SecondaryForm = [Windows.Forms.Form]::new()

$MainForm|Show-ObjectProperties
$SecondaryForm|Show-ObjectProperties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM