PowerShell-从组合框中获取值,所选值为空

PowerShell-从组合框中获取值,所选值为空,powershell,Powershell,1.csv: 服务器名 服务器1 服务器2 服务器3 我想将csv文件导入combobox,并将所选值导入变量。 我可以将上述文件加载到combobox,但输出变量为null function button ($WF) { ###################Load Assembly for creating form & button###### [void][System.Reflection.Assembly]::LoadWithPartialName( “System.W

1.csv:

服务器名

服务器1

服务器2

服务器3

我想将csv文件导入combobox,并将所选值导入变量。 我可以将上述文件加载到combobox,但输出变量为null

function button ($WF) {

###################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 = 190;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.ControlBox = $True


##############Define text label2

$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 80;

$textLabel2.Text = $WF;


############Define text box2 for input

$cBox2 = New-Object “System.Windows.Forms.combobox”;
$cBox2.Left = 150;
$cBox2.Top = 80;
$cBox2.width = 200;


###############"Add descriptions to combo box"##############
 $NameHash = @{}
import-csv "C:\Users\Komp\Desktop\1.csv" | ForEach-Object {
    $cBox2.Items.Add($_.ServerName)
    
}


#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 45;
$button.Width = 100;
$button.Text = “Ok”;
$Button.Cursor = [System.Windows.Forms.Cursors]::Hand
$Button.Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::BOLD)
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$cBox2.Text;
$form.Close();};
  
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel2);
$form.Controls.Add($cBox2);
$ret = $form.ShowDialog();

#################return values

$output = $cBox2.SelectedItem.ToString()

}


$return = button “Job Descriptions"
问题是
$output
变量为空,如何将所选值导出到
$output
变量中


多亏了圣地亚哥·斯夸松的链接,从问题中得到了这段代码,并设法解决了它

###################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 = 190;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
$form.ControlBox = $True


##############Define text label2

$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 80;

$textLabel2.Text = $WF;


############Define text box2 for input

$cBox2 = New-Object “System.Windows.Forms.combobox”;
$cBox2.Left = 150;
$cBox2.Top = 80;
$cBox2.width = 200;


###############"Add descriptions to combo box"##############
 $NameHash = @{}
import-csv "C:\Users\Komp\Desktop\1.csv" | ForEach-Object {
    $cBox2.Items.Add($_.ServerName)
    
}


#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 45;
$button.Width = 100;
$button.Text = “Ok”;
$Button.Cursor = [System.Windows.Forms.Cursors]::Hand
$Button.Font = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::BOLD)
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$cBox2.Text;
$form.Close();};
$button.Add_Click($eventHandler) ;

#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel2);
$form.Controls.Add($cBox2);
#$ret = $form.ShowDialog();

#################return values
$button.add_Click({
    #$output =  $cBox2.SelectedItem.Text #Your answer here
    Set-Variable -Name locationResult -Value $combobox1.selectedItem -Force -Scope Script # Use this
    $script:locationResult = $cBox2.selectedItem # or this to retrieve the user selection
})

$form.Controls.Add($button)
$form.Controls.Add($cBox2)

$form.ShowDialog()

Try
$output=$cBox2.SelectedItem.Text
是否回答了您的问题?@怀疑论者,没有尝试过,仍然是空的这是作用域的问题,请尝试我的链接答案。谢谢!您正在使用这两种方法来检索组合框选择,仅使用一种,我为任何人添加了这两种方法以决定使用哪一种:P。它是
设置变量
$script:Var
,我也会将代码合并到一个
$按钮中。添加(单击)
事件处理程序。