我正在使用此函数来调用Popup来插入信息:
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
}
这按我预期的那样效果很好,但是当它完成并使用“ return” cmdlet时,它将返回“ Cancel”,然后在文本框中插入信息。 杀了我,但我真的不明白是什么返回“取消”字符串。
可能是什么问题?
答案 0 :(得分:1)
Cancel
来自ShowDialog方法,该方法自动将结果返回给调用方。对于PowerShell,这意味着它将自动显示在控制台中。
要抑制它,请执行以下操作:
$form.ShowDialog() | Out-Null
如果您确实想要结果,但想将其设置为其他内容,则可以在关闭之前在事件处理程序中完成操作(并从上方省略Out-Null
):
$ConfirmBFunc = [System.EventHandler]{
$TextBox1.Text
$form.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Close()
}