title: Powershell multiple dynamic buttons
body: <p>I built a dynamic Powershell GUI but i have trouble getting my buttons to work correctly.  </p>
  
  <p>I created a function to add texboxes and buttons. However they do not correspond correctly at this point. because i'm not sure how to bind the <code>add_click</code> to a specific textbox.</p>
  
  <p>Here is the example code:</p>
  
  <pre><code>Add-Type -AssemblyName System.Windows.Forms
  Add-Type -AssemblyName System.Drawing
  
  $Form = New-Object System.Windows.Forms.Form    
  $Form.Size = New-Object System.Drawing.Size(300,300)  
  
  $ButtonAdd = New-Object System.Windows.Forms.Button
  $ButtonAdd.Location = New-Object System.Drawing.Point(20,20)
  $ButtonAdd.Size = New-Object System.Drawing.Size(50,30)
  $ButtonAdd.Text = 'Add'
  $Form.Controls.Add($ButtonAdd)
  
  $global:Counter = 0
  $global:ButtonPosY = 20
  $global:DefaultTextValue = ""
  
  $ButtonAdd.Add_Click{Add-Fields}
  
  Function Create-Button {
      param ( $ButtonPosY, $TextBoxPosY)
  
      $TextBoxField = New-Object System.Windows.Forms.TextBox
      $TextBoxField.Location = New-Object System.Drawing.Point(181,$TextBoxPosY)
      $TextBoxField.Size = New-Object System.Drawing.Size(50,30)
      $TextBoxField.Text = $global:DefaultTextValue
      New-Variable -name TextBoxField$global:Counter -Value $TextBoxField -Scope Global -Force
      $Form.controls.Add($((Get-Variable -name TextBoxField$global:Counter).value))
  
      $ButtonClick = New-Object System.Windows.Forms.Button
      $ButtonClick.Location = New-Object System.Drawing.Point(100,$global:ButtonPosY)
      $ButtonClick.Size = New-Object System.Drawing.Size(50,30)
      $ButtonClick.Text = 'Click'
      New-Variable -name ButtonClick$global:Counter -Value $ButtonClick -Scope Global -Force
      $Form.controls.Add($((Get-Variable -name ButtonClick$global:Counter).value))
  
      $ButtonClick.Add_Click({
          $((Get-Variable -name TextBoxField$global:Counter -Scope Global).value).Text = 'hello'
      })
  }
  
  Function Add-Fields {
  
      $global:Counter = $global:Counter + 1
      $global:ButtonPosY = $global:ButtonPosY + 40
  
      Create-Button -TextBoxPosY $global:ButtonPosY -ButtonPosY $global:ButtonPosY
  
  }
  
  Create-Button -TextBoxPosY 21 -ButtonPosY 20
  
  $Form.ShowDialog()
  </code></pre>
  
  <p>If the <code>click</code> button is pressed each time after new input is added everything works fine. however if multiple input fields are added first and then the <code>click</code> button is pressed the code breaks. </p>
  
  <p>The Problem is here:</p>
  
  <pre><code>    $ButtonClick.Add_Click({...})
  </code></pre>
  
  <p>I don't know how to add the counter (in my case <code>$global:Counter</code>) to the <code>$ButtonClick</code> variable as in <code>$ButtonClick0, $ButtonClick1, ... etc</code>. So right now when i add more buttons by calling the function the input will always be applied to the last added textbox since the <code>add_click</code> is not linked to a individual <code>$ButtonClick0</code> variable.</p>
  
  <p>How would this be done right?</p>