Home > other >  Display WinForms object properties in Out-GridView
Display WinForms object properties in Out-GridView

Time:08-21

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

CodePudding user response:

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 out-gridview screenshot

CodePudding user response:

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
  • Related