0

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.

4

1 回答 1

0

这是我的建议:

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

我在 PS 中有一个 Windows 窗体应用程序,我必须使用 item() 方法来访问工具栏中的按钮。如果您的情况需要数组索引,请替换.item($_)[$_].

注意方法调用getclosure()这是必要的,以便在调用 add_click 时 $_ 的值是“保留的”,直到显示 GUI 时。否则,只有在显示 GUI 时,其索引恰好具有 $i 值(显然是 0)的框才会设置其 Text 成员。

于 2013-07-03T05:11:22.550 回答