Powershell: Output a search from a csv to a small gui

Viewed 28

maybe one of you experts can help a complete newbie (I don't know if what I want is even feasible). Let's assume I have a CSV file with various data. (see csv_screenshot)csv_screenshot

I import this data via Powershell into a small GUI . How can I make it so that when I search for "Paris", I really only get the output for Paris in the GUI as a list view like this (see powershell_screenshot)

powershell_screenshot

Currently the output in the GUI looks like this (see current_result.png). How do I get it nicely formatted as a list in there. I really want to insert it like this (via Out Grid View it is no problem) current_result.png

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles();

function search_csv {
    $Input = $textbox_Search.text
    $Input = "*$Input*"
    $Input_Result  = import-csv -path C:\Users\check.csv -Header "Location", "Client", "Mobile Device" 
    $output_TextBox.text = $Input_Result -like $Input 
   
  
}

$search_csvtool = New-Object System.Windows.Forms.Form
    $search_csvtool.Text = "CSV Search"
    $search_csvtool.Size = New-Object System.Drawing.Size(674,500)
    $search_csvtool.FormBorderStyle ="FixedDialog"
    $search_csvtool.TopMost = $true
    $search_csvtool.MaximizeBox = $false
    $search_csvtool.MinimizeBox = $true
    $search_csvtool.ControlBox = $true
    $search_csvtool.StartPosition = "CenterScreen"
    $search_csvtool.Font = "Courier New"

$label_Search = New-Object System.Windows.Forms.Label
    $label_Search.Location = New-Object System.Drawing.Size(195,18)
    $label_Search.Size = New-Object System.Drawing.Size(265,32)
    $label_Search.TextAlign ="MiddleCenter"
    $label_Search.Text = "Please enter "
    $search_csvtool.Controls.Add($label_Search)

$textbox_Search = New-Object System.Windows.Forms.TextBox
    $textbox_Search.Location = New-Object System.Drawing.Size(195,50)
    $textbox_Search.Size = New-Object System.Drawing.Size(266,37)
    $search_csvtool.Controls.Add($textbox_Search)

$button_Search = New-Object System.Windows.Forms.Button
    $button_Search.Location = New-Object System.Drawing.Size(195,80)
    $button_Search.Size = New-Object System.Drawing.Size(266,24)
    $button_Search.TextAlign = "MiddleCenter"
    $button_Search.Text = "Search"
    $button_Search.Add_Click({search_csv})
    $search_csvtool.Controls.Add($button_Search)

$output_TextBox = New-Object System.Windows.Forms.TextBox
    $output_TextBox.Multiline = $true;
    $output_TextBox.Location = New-Object System.Drawing.Size(16,130)
    $output_TextBox.Size = New-Object System.Drawing.Size(627,314)
    $output_TextBox.ScrollBars = "Vertical"
    $output_TextBox.ReadOnly = $true;
    $search_csvtool.Controls.Add($output_TextBox)

    $search_csvtool.Add_Shown({$search_csvtool.Activate()})
    [void] $search_csvtool.ShowDialog()
1 Answers

Ok, so here's what I meant in my comment:

Start the code with

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$csvData = Import-Csv -path 'C:\Users\check.csv' -Header "Location", "Client", "Mobile Device" 

function search_csv {
    $searchThis = $textbox_Search.Text
    # use $script: scoping here to reference the $csvData variable
    $data = $script:csvData | Where-Object {$_.Location -like "*$searchThis*"}
    if ($data) {
        $output_TextBox.Text = ($data | Format-List | Out-String).Trim()
    }
    else {
        $output_TextBox.Text = "Not found.."
    }
}

Then create the rest of the form as you did.
Important: Destroy the form when done with a last new code line:

$search_csvtool.Dispose()

You should then have this result:

enter image description here

Related