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

  • Question

  • 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!



    Monday, February 16, 2015 7:06 PM

Answers

  • 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


    ¯\_(ツ)_/¯



    • Edited by jrv Monday, February 16, 2015 7:57 PM
    • Marked as answer by Ivory Mischke Monday, February 16, 2015 8:17 PM
    Monday, February 16, 2015 7:54 PM
  • 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()


    ¯\_(ツ)_/¯

    • Marked as answer by Ivory Mischke Monday, February 16, 2015 8:18 PM
    Monday, February 16, 2015 8:05 PM

All replies

  • 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()


    ¯\_(ツ)_/¯

    • Marked as answer by Ivory Mischke Monday, February 16, 2015 8:18 PM
    • Unmarked as answer by Ivory Mischke Monday, February 16, 2015 8:18 PM
    Monday, February 16, 2015 7:42 PM
  • 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


    ¯\_(ツ)_/¯



    • Edited by jrv Monday, February 16, 2015 7:57 PM
    • Marked as answer by Ivory Mischke Monday, February 16, 2015 8:17 PM
    Monday, February 16, 2015 7:54 PM
  • Works 100% thanks for the quick response!

    Monday, February 16, 2015 7:59 PM
  • 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()


    ¯\_(ツ)_/¯

    • Marked as answer by Ivory Mischke Monday, February 16, 2015 8:18 PM
    Monday, February 16, 2015 8:05 PM