0

I am using this function to invoke Popup to insert information:

function Set-Popup ([String]$Title, [String]$Label) {

###################Load Assembly for creating form & button

[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)

#####Define the form size & 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
}

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.

Here I inserted CompTest in the Text Box

What can be the problem?

Lior
  • 139
  • 2
  • 13

1 Answers1

1

The Cancel is coming from the ShowDialog method, which automatically returns the result to the caller. In the case of PowerShell, that means it will automatically be displayed in the console.

To suppress it, do this:

$form.ShowDialog() | Out-Null

If you actually want the result, but want to set it to something else, you can do it in your event handler before closing (and omit the Out-Null from above):

$ConfirmBFunc = [System.EventHandler]{
        $TextBox1.Text
        $form.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $form.Close()
    }
boxdog
  • 7,894
  • 2
  • 18
  • 27
  • It still throws me a Cancel result even after writing Out-Null :( – Lior Nov 07 '18 at 15:52
  • Check my example again - I had a typo. Be sure you're putting the `Out-Null` in the correct place (after `ShowDialog`) – boxdog Nov 07 '18 at 15:54