title: Powershell Windows Form listbox selection validation
body: <p>I have created a powershell script using windows forms containing list boxes to run a cmd command (helps eliminate user-error and expedites process a bit).  The only problem I run into is the command will still attempt to run even if an item is not selected on one of the forms I have populate.  This can cause the script to simply not run, or it can cause mass amounts of data to be downloaded (the script pulls log files from a server, the listbox helps narrow down the data to be pulled).  Is there a way to create error checking for the listbox that will essentially say "hey, you didn't select anything!" before continuing?
  Thanks!</p>
  
  <p>Edit (example of first list box):</p>
  
  <pre><code>$form = New-Object System.Windows.Forms.Form 
  $form.Text = "Select a production environment"
  $form.Size = New-Object System.Drawing.Size(190,250) 
  $form.StartPosition = "CenterScreen"
  
  $OKButton = New-Object System.Windows.Forms.Button
  $OKButton.Location = New-Object System.Drawing.Point(10,180)
  $OKButton.Size = New-Object System.Drawing.Size(75,23)
  $OKButton.Text = "OK"
  $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  $form.AcceptButton = $OKButton
  $form.Controls.Add($OKButton)
  
  $CancelButton = New-Object System.Windows.Forms.Button
  $CancelButton.Location = New-Object System.Drawing.Point(85,180)
  $CancelButton.Size = New-Object System.Drawing.Size(75,23)
  $CancelButton.Text = "Cancel"
  $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  $form.CancelButton = $CancelButton
  $form.Controls.Add($CancelButton)
  
  $label = New-Object System.Windows.Forms.Label
  $label.Location = New-Object System.Drawing.Point(10,20) 
  $label.Size = New-Object System.Drawing.Size(280,20) 
  $label.Text = "Please select a production environment:"
  $form.Controls.Add($label) 
  
  $listBox = New-Object System.Windows.Forms.ListBox 
  $listBox.Location = New-Object System.Drawing.Point(10,40) 
  $listBox.Size = New-Object System.Drawing.Size(150,20) 
  $listBox.Height = 140
  
  [void] $listBox.Items.Add("server1")
  [void] $listBox.Items.Add("server2")
  [void] $listBox.Items.Add("server3")
  
  
  $form.Controls.Add($listBox) 
  
  $form.Topmost = $True
  
  $result = $form.ShowDialog()
  
  if ($result -eq [System.Windows.Forms.DialogResult]::OK)
  {
      $Prod = $listBox.SelectedItem
      $Prod
  }
  </code></pre>