DarkLite1
DarkLite1

Reputation: 14705

PowerShell Find all variables of the type Forms.Panel

In the process of creating a .NET form in PowerShell, I stumbled upon an annoyance. I need to be able to deactivate the visibility of all panels except for the one that has to be displayed. To do this it seems convenient to search for all available variables of the type System.Windows.Forms.Panel and put their status to $Panelx.Visible = $False.

Example of all kinds of variables:

[String]$Stuff = 'Blabla'
$Panel1 = New-Object System.Windows.Forms.Panel
$Panel2 = New-Object System.Windows.Forms.Panel
$Panel3 = New-Object System.Windows.Forms.Panel
$Button = New-Object System.Windows.Forms.Button
$TabControl = New-object System.Windows.Forms.TabControl

This gives me the correct results, but I can't set the Visible status to $False

Get-Variable | ? {$_.Value} | ? {(($_.Value).GetType().Name) -eq 'Panel'} | % {
    $_.Visible = $false
}

How is it possible to only list the variables of the type panel and then put them on $Panel.Visible = $False?

Thank you for your help.

Upvotes: 1

Views: 614

Answers (1)

Micky Balladelli
Micky Balladelli

Reputation: 9991

I think the issue is that Get-Variable returns objects of type PSVariable, so the Panel object is defined within. Use the member property value to retrieve it, like this:

Get-Variable |where {$_.Value -is [System.Windows.Forms.Panel] } | % {$_.value.visible = $false}    

Upvotes: 2

Related Questions