Home > Software design >  How to create click events for Windows form generated recursively in powershell
How to create click events for Windows form generated recursively in powershell

Time:08-19

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;
}
  

CodePudding user response:

  • Assuming that $hash is a hashtable, as the name suggests, it is not enumerated in the pipeline, so that $_ in your $hash | ForEach-Object{ ... } command refers to $hash itself, not its entries.

    • To enumerate the latter, use $hash.GetEnumerator().
  • Inside a script block ({ ... }) serving as an event delegate, the automatic $_ variable is not defined.

  • Related