【问题标题】:PowerShell - Get value from combobox, selected value is emptyPowerShell - 从组合框中获取值,选定的值为空
【发布时间】:2021-04-21 17:56:31
【问题描述】:

1.csv:

服务器名称

服务器1

服务器2

服务器3

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

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变量中?

this 问题获得此代码

【问题讨论】:

  • 试试$output = $cBox2.SelectedItem.Text
  • this 回答你的问题了吗?
  • @Sceptalist ,还没试过,还是空的
  • 这是范围的问题,试试我的链接答案。

标签: powershell


【解决方案1】:

感谢 Santiago Squarzon 的链接,设法解决了它

###################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()

【讨论】:

  • 您正在使用这两种方法来检索comboBox 选择,只使用一种,我添加了两种方法供任何人决定使用哪一种:P。它可以是Set-Variable$script:Var
  • 我还将代码合并到一个 $button.Add_Click(..) 事件处理程序中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
相关资源
最近更新 更多