Dynamically Created Buttons That Pass The Same Variable With Different Values To A Function

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!



February 16th, 2015 10:06pm

That won't create a form or a button. All you are doing is rewriteing the same buton over and over.

Create the buttons and add a click to each on then add the argument.

function function-to-run($argtopass){
    write-host $argtopass
}
.....
$arguments = get-content c:\arguments.txt
foreach ($arg in $arguments){
    $b=New-Object System.Windows.Forms.Button
     $b.location= < set location>
     $b.Text="MyButton($arg)"
    $b.add_click({
         function-to-run -argtopass $arg
         $form.close()
    })
    $form.controls.add($b)
}
.....
$form.showdialog()

Free Windows Admin Tool Kit Click here and download it now
February 16th, 2015 10:42pm

Here is a fully working example including the argument:

$form=New-Object System.Windows.Forms.Form function function-to-run{
$this.ForeColor=$this.Tag write-host $this.Tag -fore $this.Tag -back white } $arguments = get-content c:\scripts\arguments.txt $i=0 foreach ($arg in $arguments){ $b=New-Object System.Windows.Forms.Button $b.Name=$arg $b.location="0,$(25*++$I)" $b.size='100,20' $b.Text="MyButton ($arg)" $b.Tag=$arg $b.add_click({ function-to-run }) $form.controls.add($b) } $form.showdialog()

For the demo to work place the following in the filr

red
green
blue
yellow

February 16th, 2015 10:54pm

Works 100% thanks for the quick response!

Free Windows Admin Tool Kit Click here and download it now
February 16th, 2015 10:59pm

Alternate method:

$form=New-Object System.Windows.Forms.Form
function function-to-run($arg){
    $this.ForeColor=$arg
    write-host $arg -fore  $arg -back white
}

$arguments = get-content c:\scripts\arguments.txt
$i=0
foreach ($arg in $arguments){
    $b=New-Object System.Windows.Forms.Button
    $b.Name=$arg
     $b.location="0,$(25*++$I)"
      $b.size='100,20'
     $b.Text="MyButton ($arg)"
     $b.add_click([scriptblock]::Create("function-to-run $arg"))
    $form.controls.add($b)
}
$form.showdialog()

February 16th, 2015 11:05pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics