Cheries
Cheries

Reputation: 892

How to append ComboBox Selected File to TextBox using Powershell?

I have a GUI with combobox and textbox. I need to update the output of textbox consider to combobox selected file.

I tried this. But the output in the textbox is not correct. Anyone can help please. THank you

$Disk = New-Object system.Windows.Forms.ComboBox
$Disk.AutoSize = $true
$Disk.DropDownStyle = "DropDownList"

$Disk.Items.AddRange((get-partition|?{$_.DriveLetter}|select DiskNumber ))
$SelectedFile= {
    $DiskSelected = $Disk.SelectedItem
    Write-Host "Disk Selected: $DiskSelected"

    $DiskNum = $DiskSelected -split "@{DiskNumber="
    $DiskNumber = $DiskNum -split "}"
    Write-Host "Disk Number: $DiskNumber"

    $TextBox1.AppendText((Get-Disk -Number $DiskNumber))
}

$Disk.add_SelectedIndexChanged($SelectedFile)
$Disk.DisplayMember = 'Name'

$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $true
$TextBox1.BackColor = "#F5F5F5"

The output in the textbox is like this

MSFT_Disk (ObjectId = "{1}\\SSXX\root/Microsoft/Windows/Stor...) MSFT_Disk (ObjectId = "{1}\\SSXX\root/Microsoft/Windows/Stor...)

Updated

Now, I can bring the disk information to textbox, but can the textbox does not updated if I select another item in the combobox.

$Disk = New-Object system.Windows.Forms.ComboBox
$Disk.AutoSize = $true
$Disk.DropDownStyle = "DropDownList"

$Disk.Items.AddRange((get-partition|?{$_.DriveLetter}| select DiskNumber )) 
$SelectedFile = {
    $DiskSelected = $Disk.SelectedItem
    Write-Host "Disk Selected: $DiskSelected"

    $DiskNum = $DiskSelected -split "@{DiskNumber="
    $DiskNumber = $DiskNum -split "}"
    Write-Host "Disk Number:$DiskNumber"
    Get-Disk | Where-Object -FilterScript {$_.Number -Eq "$DiskNumber"} | Select FriendlyName, BusType, Size | Export-Csv 'Temp.csv' -NoType
    $TextBox1.AppendText((Get-Content -Path ".\Temp.csv"))
}

$Disk.add_SelectedIndexChanged($SelectedFile)
$Disk.DisplayMember = 'Name'

$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $true
$TextBox1.WordWrap = $true
$TextBox1.BackColor = "#F5F5F5"
$textsize = ($width / 130)
$FontText = New-Object System.Drawing.Font("Calibri",$textsize,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Pixel)
$TextBox1.Font = $FontText

Upvotes: 2

Views: 670

Answers (1)

rokumaru
rokumaru

Reputation: 1244

Try this.

using namespace System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms

$partitions = Get-Partition | where DriveLetter

########## Controls ##########

$comboBox1 =[ComboBox]@{
    Location = "10,10"
    AutoSize = $true
    DropDownStyle = "DropDownList"
    DisplayMember = "DriveLetter"
}
$comboBox1.Items.AddRange($partitions)

$textBox1 = [TextBox]@{
    Location = "10,50"
    Size = "900,300"
    Anchor = "Left,Right,Top,Bottom"
    Multiline = $true
    WordWrap = $false
    ScrollBars = "Both"
    BackColor = "#F5F5F5"
    Font = "Consolas,10"
}

$form = [form]@{ Size = "940,400" }
$form.Controls.AddRange(@($comboBox1, $textBox1))


########## EventHandlers ##########

$comboBox1.add_SelectedIndexChanged{
    $selectedPartition = $comboBox1.SelectedItem
    $TextBox1.Text = @(
        $selectedPartition.DriveLetter + ":"
        Get-Disk -Partition $selectedPartition | Format-List | Out-String
    )
}

# Show form
[void]$form.ShowDialog()

Note that the item you select is an object, not a string.


updated

Try rewriting the following part.
You can update all text by $TextBox1.Text = ... instead of $TextBox1.AppendText().

$Disk.Items.AddRange((get-partition|?{$_.DriveLetter}| select -ExpandProperty DiskNumber)) 
$SelectedFile = {
    $DiskNumber = $Disk.SelectedItem
    Write-Host "Disk Number:$DiskNumber"
    $TextBox1.Text = Get-Disk $DiskNumber | Select FriendlyName, BusType, Size | Out-String
}

Upvotes: 1

Related Questions