title: Need help regarding creating dynamic forms in powershell
body: <p>Hi i am trying to create a dynamic form in pwoershell , this is a form which has 5 buttons(color names) and each button opens a different text file(such as if red button is clicked,it should open red.txt; here is the full code;</p>
  
  <h1>Script Start</h1>
  
  <pre><code>$var = "Red","Blue","Yellow","Black","White"
  
  
  $testForm = New-Object System.Windows.Forms.Form
  $testForm.Text = "Color List"
  $testForm.AutoSize = $True
  $testForm.AutoSizeMode = "GrowAndShrink"     
  
  $Font = New-Object System.Drawing.Font("Times New Roman",24, [System.Drawing.FontStyle]::Bold)   
  $testForm.Font = $Font
  
  
  $Label = New-Object System.Windows.Forms.Label
  $Label.Text = "Select the Color"
  $Label.AutoSize = $True
  $testForm.Controls.Add($Label)
  
  
  $x=100
  $y=50
  foreach($color in $var)
  {
      $run = New-Object System.Windows.Forms.Button
      $run.Location = New-Object System.Drawing.Size($x,$y)
      $run.Size = New-Object System.Drawing.Size(100,50)
      $run.Text = "$Color"
      $run.Add_Click({ Invoke-Expression "notepad C:\Users\User\$color.txt" })
      $testForm.Controls.Add($run)
      $Font = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Regular)
      $run.font = $Font
      $run.AutoSize = $True
      $y+=50
  
  }
  $testForm.ShowDialog()
  </code></pre>
  
  <h1>END Script</h1>
  
  <p>Everything went fine until, when the form opens with buttons, and all the buttons when clicked, open the file "White.txt" since its the last element in the array; is there any way to change the script to make each button open only their respective files and not the last color file?</p>
  
  <p>please do let me know if any further questions or clarifications needed.</p>