用csv显示成员填充组合框

时间:2014-11-11 15:46:38

标签: powershell csv combobox bind

我创建了一个带有组合框的简单窗体,我试图在我的csv中填写全名列,我希望它在变量中返回缩减的名称列值,所以我使用了一个哈希表并添加了两个列,但是从组合框中选择一个项目后,$ namehash [$ return]为空。它不应该返回已删除的名字吗?我在c#和VB中找到了使用Combobox.DisplayMember,Combobox.ValueMember,Combobox.DataSource的其他示例。我怎么能在PowerShell中做到这一点?

我的csv是逗号分隔

姓名,短名称

密歇根州,MI

俄亥俄,OH

Import-Module Activedirectory


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:\temp\test_cidb.csv' | ForEach-Object {
    $cBox2.Items.Add($_.Description)
    $NameHash.Add($_.Name,$_.Shortname)
}

############Define text box3 for input

#############Define default values for the input boxes
$defaultValue = “”
$cBox2.Text = $defaultValue;
#############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

return $cbox2.text

}

$return= button “Job Descriptions"

我通过使用它作为数据源创建数据表并使用displaymember,valuemember,datasource类绑定组合框来解决我自己的问题。

$reader = new-object System.IO.StreamReader($csvfile) 
$line = $reader.ReadLine() 
$columns =  $line.Split($csvdelimiter) 

foreach ($column in $columns) {$null = $datatable.Columns.Add($column) }
   while (($line = $reader.ReadLine()) -ne $null)  { 
        $row = $datatable.NewRow() 
        $row.itemarray = $line.Split($csvdelimiter) 
        $datatable.Rows.Add($row)   
}
$cbox2.DisplayMember = "Name"
$cbox2.ValueMember = "Shortname"
$cbox2.DataSource = $datatable

$States=cbox2.SelectedValue

2 个答案:

答案 0 :(得分:0)

问题是您无法返回已添加到哈希的内容。一旦返回函数,它就会丢失。因此,您需要首先在函数外部定义$ namehash。

...
return $cbox2.text
}
$NameHash=@{}
$return= button “Job Descriptions"

然后,删除函数中的$ NameHash变量,并使用脚本中的那个:

###############"Add descriptions to combo box"##############
##  $NameHash = @{}
import-csv 'C:\temp\a.csv' | ForEach-Object {
    $cBox2.Items.Add($_.Description)
    ## $NameHash.Add($_.Name,$_.Shortname)
    $Script:NameHash.Add($_.Name,$_.Shortname)
}

答案 1 :(得分:0)

$reader = new-object System.IO.StreamReader($csvfile) 
$line = $reader.ReadLine() 
$columns =  $line.Split($csvdelimiter) 

foreach ($column in $columns) {$null = $datatable.Columns.Add($column) }
   while (($line = $reader.ReadLine()) -ne $null)  { 
        $row = $datatable.NewRow() 
        $row.itemarray = $line.Split($csvdelimiter) 
        $datatable.Rows.Add($row)   
}
$cbox2.DisplayMember = "Name"
$cbox2.ValueMember = "Shortname"
$cbox2.DataSource = $datatable

$States=cbox2.SelectedValue