title: creating GUI forms without variables
body: <p>I am trying to find a way to create a form in PowerShell without using any variables unless they are temporarily or virtually assigned. I want to be able to run a command similar to this:</p>
  
  <pre><code>(New-Object System.Windows.Forms.Form).ShowDialog()
  </code></pre>
  
  <p>where I can enter in a code into an event that is triggered once the form is created. That event will then be responsible for creating all the objects and other events inside the form. Once the form is launched, I will not need any variables accept for the ones that are virtually assigned within the events.</p>
  
  <p>This to avoid using too much system resources from assigning and endless amount of variables for each object in the form. The script that I am currently working on in PowerShell is very possibly going to be really big, and even if it is not a very large script, efficiency and clean code is always the key to writing a good program or script. </p>
  
  <pre><code>add-type -ass System.Windows.Forms
  $x = (New-Object System.Windows.Forms.Form)
  $x.Text = 'Message Box'
  $x.Size = '300,150'
  $x.Font = $x.Font.Name + ',12'
  $x.Controls.Add((New-Object System.Windows.Forms.Label))
  $x.Controls[-1].Size = $x.Size
  $x.Controls[-1].Text = 'Here is a message for you'
  $x.ShowDialog()
  Remove-Variable x
  </code></pre>
  
  <p>It is very possible to access these objects still with the exact same kind of access when you define each object with a variable. It cost me many hours of research and just simply attempting random commands to find out how to do this. Here is all the commands you may need to relearn if you are interested in my solution:</p>
  
  <pre><code># create item in form:
  $x.Controls.Add((New-Object System.Windows.Forms.Button))
  # access the last created item in the form:
  $x.Controls[-1]
  # change it's name to identify it easier
  $x.Controls[-1].Name = 'button1'
  # access the item by it's new name:
  $x.Controls['button']
  # delete the item by it's name:
  $x.Controls.Remove($x.Controls['button1'])
  </code></pre>
  
  <p>If your familiar with form creation in PowerShell then this should all make sense to you and you should be familiar with how the rest of it works. Also, another note to make for those who are interested in what I am trying to do is that any of these commands can be done within an event by replacing <code>$x</code> with <code>$this</code>. If it is inside an event of an object inside the "controls" section of the form, then you would use <code>$this.parent</code>.</p>
  
  <p>This is exactly what I mean by having the ability to create a form with virtually no variables. The only problem I am having with this is that I am unsure how to assign an event and call the method <code>ShowDialog()</code> at the same time.</p>