none
Procedurally creating buttons in Powershell RRS feed

  • Question


  • Hi, hoping you can help. I've run into the same issue with this piece of code (pretty similar to the OP),

    and I've now drawn a mental blank:

    ------


    # Drop the respective site buttons onto the form using a loop.

     $i=0
    while($i -lt $count1)
      {
       #Label each button with site name.
       $Text = $sites[$i] | ForEach {$_.Name}
     
       $SiteButton = New-Object System.Windows.Forms.Button
       $SiteButton.Location = New-Object System.Drawing.Size(15,$yloc)
       $SiteButton.Size = New-Object System.Drawing.Size(100,23)
       $SiteButton.Text = $Text
       $SiteButton.Add_Click({$outputBox.Text=$SiteButton.Text})
       $objForm.Controls.Add($SiteButton)
       $i+=1
       $yloc=$yloc+$yloc_incr
      }

    I click on any of the buttons that are created (which are named with the right array names), but

    it returns the name of the very last button each time into the outputbox. I've tried

    a number of things without success, to return the name of the actual button selected.

    Ultimately I'd like to call a function also, but my function even returns the last button's text as the passed

    argument. ($Text). Hopefully it's something simple...

    Wednesday, June 17, 2015 1:26 AM

Answers

  • You cannot keep calling the button by name as it will always refer to the last one created. You have to call it by its "Me" reference:

    Add-Type -AssemblyName System.Windows.Forms
    $form=New-Object System.Windows.forms.Form
    $outputbox=New-Object System.Windows.Forms.TextBox
    $outputbox.Location='150,20'
    $form.Controls.Add($outputbox)
    $count=10
    for($i=1;$i -le $count;$i++){
        $SiteButton=New-Object System.Windows.Forms.Button
        $SiteButton.Location="15, $((20*$i) + 5)"
        $SiteButton.Size='100, 20'
        $SiteButton.Text = "Site  - $i"
        $SiteButton.Add_Click({ $outputBox.Text = $this.Text })
        $form.Controls.Add($SiteButton)
    }
    
    $form.ShowDialog()


    \_(ツ)_/


    • Edited by jrv Wednesday, June 17, 2015 2:08 AM
    • Marked as answer by shades4321 Wednesday, June 17, 2015 2:56 AM
    Wednesday, June 17, 2015 2:08 AM

All replies

  • You cannot keep calling the button by name as it will always refer to the last one created. You have to call it by its "Me" reference:

    Add-Type -AssemblyName System.Windows.Forms
    $form=New-Object System.Windows.forms.Form
    $outputbox=New-Object System.Windows.Forms.TextBox
    $outputbox.Location='150,20'
    $form.Controls.Add($outputbox)
    $count=10
    for($i=1;$i -le $count;$i++){
        $SiteButton=New-Object System.Windows.Forms.Button
        $SiteButton.Location="15, $((20*$i) + 5)"
        $SiteButton.Size='100, 20'
        $SiteButton.Text = "Site  - $i"
        $SiteButton.Add_Click({ $outputBox.Text = $this.Text })
        $form.Controls.Add($SiteButton)
    }
    
    $form.ShowDialog()


    \_(ツ)_/


    • Edited by jrv Wednesday, June 17, 2015 2:08 AM
    • Marked as answer by shades4321 Wednesday, June 17, 2015 2:56 AM
    Wednesday, June 17, 2015 2:08 AM
  • Got it. Thanks. I wasn't taking the "$this" literally. : ) Now working.

    Thanks so much JRV.


    • Edited by shades4321 Wednesday, June 17, 2015 2:55 AM
    Wednesday, June 17, 2015 2:21 AM