title: Cancel return result from gui popup
body: <p>I am using this function to invoke Popup to insert information:</p>
  
  <pre><code>function Set-Popup ([String]$Title, [String]$Label) {
  
  ###################Load Assembly for creating form &amp; button
  
  [void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
  [void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
  
  #####Define the form size &amp; placement
  
  $form = New-Object “System.Windows.Forms.Form”;
  $form.Width = 500;
  $form.Height = 150;
  $form.Text = $title;
  $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
  
  ##############Define text label1
  $TextLabel1 = New-Object “System.Windows.Forms.Label”;
  $TextLabel1.Left = 25;
  $TextLabel1.Top = 15;
  $TextLabel1.Width = 200;
  
  $TextLabel1.Text = "$Label";
  
  ##############Define error label
  
  $ErrorLabel = New-Object “System.Windows.Forms.Label”;
  $ErrorLabel.Left = 25;
  $ErrorLabel.Top = 65;
  $ErrorLabel.Width = 450;
  
  ############Define text box1 for input
  $TextBox1 = New-Object “System.Windows.Forms.TextBox”;
  $TextBox1.Left = 250;
  $TextBox1.Top = 10;
  $TextBox1.width = 200;
  
  #############define Confirm button
  $ConfirmButton = New-Object “System.Windows.Forms.Button”;
  $ConfirmButton.Left = 360;
  $ConfirmButton.Top = 85;
  $ConfirmButton.Width = 100;
  $ConfirmButton.Text = “Confirm”;
  
  #############define Cancel button
  $CancelButton = New-Object “System.Windows.Forms.Button”;
  $CancelButton.Left = 250;
  $CancelButton.Top = 85;
  $CancelButton.Width = 100;
  $CancelButton.Text = “Cancel”;
  
  ############# This is when you have to close the form after getting values
  $ConfirmBFunc = [System.EventHandler]{
  $TextBox1.Text;
  $form.Close();
  };
  
  $ConfirmButton.Add_Click($ConfirmBFunc);
  
  #############Add controls to all the above objects defined
  $form.Controls.Add($ConfirmButton);
  $form.Controls.Add($TextLabel1);
  $form.Controls.Add($ErrorLabel);
  $form.Controls.Add($TextBox1);
  $form.ShowDialog();
  
  #################return values
  
  return $TextBox1.Text
  }
  </code></pre>
  
  <p>This works great as I was expecting but when it finishes and using the "return" cmdlet, it returns "Cancel" and after that the info I inserted in the text box.
  Kill me but i really don't understand what returns the "cancel" string.</p>
  
  <p><a href="https://i.stack.imgur.com/mtdt5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mtdt5.png" alt="Here I inserted CompTest in the Text Box"></a></p>
  
  <p>What can be the problem?</p>