locked
fill combobox from CSV using hashtable RRS feed

  • Question

  • Hi, i created a simple windows form with a combobox that i im trying to fill with the full name column in my csv and i want it to return the  abreviated name column value in a variable so i used a hashtable and added both columns, but after selecting an item from the combobox, the $namehash[$return] is null. Shouldnt it return the abreviated name?

    My csv is comma seperated

    Name, Shortname

    Michigan,MI

    Ohio,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"

    Tuesday, November 11, 2014 2:58 PM

Answers

  • I solved my own issue by creating a datatable using that as the data source and binded the combobox using displaymember, valuemember, datasource classes. Exactly what I was wanting do do.

    $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
    
    • Marked as answer by glacket Wednesday, November 12, 2014 8:08 PM
    Wednesday, November 12, 2014 8:08 PM