Dynamically add and remove content in GUI form
<p>I am trying to create in PowerShell a GUI window with dynamic content. I need to:</p>

<ul>
<li>create a window with a random count of buttons (or other clickable items)</li>
<li>after a click button and related text label will be removed from the window</li>
</ul>

<p>IMPORTANT: I cannot use a list or datagrid.</p>

<p>I have the following code but it still returns only last item value.</p>

<pre><code>[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(400,400)

$Array = New-Object System.Collections.ArrayList
$Array.add('AAA')
$Array.add('BBB')
$Array.add('CCC')
$Array.add('DDD')

foreach ($item in $Array) {
  New-Variable -Force -Name "button$membershipCount" -Value (New-Object System.Windows.Forms.Button)
  $thisButton = Get-Variable -ValueOnly -Include "button$membershipCount"
  $thisButton.Location = New-Object System.Drawing.Size(175,(35+26*$membershipCount))
  $thisButton.Size = New-Object System.Drawing.Size(250,23)
  $thisButton.Text = $item
  $thisButton.Add_Click({(Write-Host $thisButton.Text | Out-Null)})

  $form.Controls.Add($CHANGEButton)
}
</code></pre>

<p>I also tried <code>Invoke-Expression</code>, but it doesn't return expected results:</p>

<pre><code>Invoke-Expression -Command "`$thisButton.Add_Click({`$x=`"$($item)`";`write-host $x})"
</code></pre>

<p>Or any better idea how can I get details which button was clicked since the number of buttons is random?</p>