简体   繁体   中英

Powershell Dynamic GUI

I am building a dynamic GUI in powershell but having some problems The objects are created based on folders in the current directory

$i = 0 
$arrbuttons | %{
$_.add_Click({$arrboxes[$i].Text = "hello"})
$i++
}

All my buttons are located in $arrbuttons array and all my textboxes in the $arrboxes array If i set the $i to 0 and dont addition at the end everything works fine but the text always gets in the first textbox right? But when i addition at the end i get an exception saying that property text cannot be found.

What over all I am trying to achieve is this just a small problem along the way to build a dynamic GUI with buttons textboxes and a stopwatch for each row, but I am not sure on how to dynamically create stopwatches and make the script understand which stopwatch to stop and start when pressed the individual buttons.

Textbox,Start,Stop,Reset

Textbox,Start,Stop,Reset

You get it so when pressing start textbox will say something like "tick tock" and when pressing stop the elapsed time will display in the correct textbox.

Here's what I'd suggest:

0..($arrbuttons.count) | % {
  $arrbuttons.item($_).add_Click({$arrboxes.item($_).Text = "hello"}.getclosure())
}

I have a Windows form app in PS, and I have to use the item() method to access buttons in a toolbar. If array indexing is needed in your situation then replace .item($_) with [$_] .

Note the method call getclosure() That is necessary so that the value of $_ at the time add_click is called is "preserved" till the time the GUI is displayed. Otherwise only the box whose index happens to have the value of $i - which is evidently 0 - when the GUI is displayed will get its Text member set.

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