I am working on a Powershell form that will have a varying amount of buttons and ideally each button will pass a different value to a function. I am able to create the buttons and place them on the form but can't get the proper value of the variable to pass into the function. This is what I have tried so far:
$buttonlocation = 1
$button = @{}
$arguments = get-content{c:\arguments.txt}
function function-to-run($argtopass)
{write-host $argtopass}
foreach ($arg in $arguments)
{
$button[$a] = menu-button
$button[$a].add_click({function-to-run -argtopass $arg;$form.close()})
$form.controls.add($button[$a])
$a = $a + 1
}
$form.showdialog()
This will return the value of $arg at the end of the foreach loop and not what $arg was at the time of adding it to the form. So here is another try:
$button[$a].add_click({function-to-run -argtopass $arg;$form.close()}.getnewclosure())
This will return an exception "the term 'function-to-run' is not recognized as the name of a cmdlet, function, script file, or operable program." but this will return the proper value that I want to pass:
$button[$a].add_click({write-host $arg;$form.close()}.getnewclosure())
This will return the value I want but I can't figure out how to pass this to my function. I have also tried this:
$b[$a] = $arg
$button[$a].add_click({function-to-run -argtopass $b[$a];$form.close()})
This returns an empty string on every button I press.
The code provided is just what I think is important if you need any other part of the code please let me know. Any help is appreciated. Thanks!
- Edited by Ivory Mischke Monday, February 16, 2015 7:15 PM