powershellwinformstablelayoutpanel

How to create click events for Windows form generated recursively in powershell


I am trying to populate a tree structure in Powershell windows form. The only difference is my tree structure will have textboxes, which I found is not possible using TreeView. So I am using a recursive function to populate the form step by step. However, when I try to add_click on any of the form control, it throws an error saying the object is null. I am new on this and would appreciate any suggestions on how to solve this. The exact message is

Cannot bind argument to parameter 'panel' because it is null.

My functions look like this. The click event binds successfully and calls the toggleVisible function, however at runtime when the click happens it does not pass the correct value to the function.

Function handlePanelClick{
    $hash | ForEach-Object{
       $_.Label.Add_Click({toggleVisible $_.Panel});
    }

}

Function toggleVisible{
    Param(
        [Parameter(Mandatory = $true, Position = 1)]
        [System.Object]$panel
    )
    $panel.Visible = $false;
}
  

Solution