Home > Back-end >  Hash table & TextBox
Hash table & TextBox

Time:06-12

I have the TextBox

$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,40)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)
.
.
$x = $textBox.Text

and want to include my hash table in a way the input of the TextBox corresponds to values in it.

For example :

$hash = @{
A1 = "Stick"
A2 = "Leaf"
A3 = "Tree"
}

When I type A1 into the TextBox I want it to output 'stick'.

Previously, I used

do {
    $computer = $null
    $choice = [Microsoft.VisualBasic.Interaction]::InputBox('Type the name of a computer','Test')
    # exit the loop if the user cancels the box or clicks OK with an emty value
    if ([string]::IsNullOrWhiteSpace($choice)) { break }
    
    $computer = $hash[$choice]
} until ($computer)

But I haven't figured out how to implement $choice into the script to output it correctly

help much needed

CodePudding user response:

Well, this was a fun project. Used to do a lot in C# forms, and still new to forms in PowerShell - but I've been itching for an excuse.

I found this Custom Input Box from Microsoft, but then found this answer by rokumaru. The answer given by rokumaru is, in my opinion, a far better design implementation for forms (and I had no clue it could be done that way). Then I found this answer by Reza Aghaei which uses using assembly to make types in System.Windows.Forms available.

The code below is a hybrid of the Visual Basic's InputBox layout, Microsoft's Custom Input Box's foundational code, rokumaru's design implementation plus comboBox, and Reza's using assembly.

The function Open-SelectBox takes a $Prompt string, $Title string, and $Options which is an array of strings, and opens a VB like InputBox. The $comboBox is populated with $Options and Open-SelectBox returns either $null if cancel is pressed, an empty string if OK is pressed without selecting anything, or an option provided by $Options.

There is definitely some fine tuning that could be done related to how the comboBox is populated, should it have a default value, should it be forced to only return a valid value, etc... But I feel this is a fairly good foundation code that you should be able to use in your project.

If you need some functionality changed, let me know.

using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing

function Open-SelectBox {
    [OutputType([string])]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$Prompt,
        [Parameter(Mandatory = $true, Position = 1)]
        [string]$Title,
        [Parameter(Mandatory = $true, Position = 2)]
        [string[]]$Options
    )
    $okButton = [Button]@{
        Location = '290,12'
        Size = '60,22'
        Text = 'OK'
        DialogResult = [DialogResult]::OK
    }
    $cancelButton = [Button]@{
        Location = '290,40'
        Size = '60,22'
        Text = 'Cancel'
        DialogResult = [DialogResult]::Cancel
    }
    $label = [Label]@{
        AutoSize = $true
        Location = '10,10'
        Size = '240,20'
        MaximumSize = '250,0'
        Text = $Prompt
    }
    $comboBox =[ComboBox]@{
        Location = '10,90'
        AutoSize = $true
        MaximumSize = '335,0'
        DropDownStyle = "DropDownList"
        ValueMember = "A2"
    }
    $comboBox.Items.AddRange($Options)
    $SelectBox = [form]@{
        Text = $Title
        Size = '369,159'
        StartPosition = 'CenterScreen'
        AcceptButton = $okButton
        CancelButton = $cancelButton
        FormBorderStyle = 'FixedDialog'
        MinimizeBox  = $false
        MaximizeBox = $false
    }
    $SelectBox.Controls.AddRange(@($okButton, $cancelButton, $label, $comboBox))
    $SelectBox.Topmost = $true
    $SelectBox.Add_Shown({$comboBox.Select()})
    $result = $SelectBox.ShowDialog()
    if ($result -eq [DialogResult]::OK) {
        return $comboBox.Text
    } else {
        return $null
    }
}
$hash = @{
    A1 = "Stick"
    A2 = "Leaf"
    A3 = "Tree"
}
$Key = Open-SelectBox 'Type the name of a computer' 'Test' $hash.Keys
if($null -eq $Key) {
    Write-Host 'Cancel pressed!'
} elseif( '' -eq $Key) {
    Write-Host "Nothing selected!"
} else {
    Write-Host "$($hash[$Key])"
}

CodePudding user response:

Found it out yesterday. The answer to my problem was:

  $hash = @{
    A1 = "Stick"
    A2 = "Leaf"
    A3 = "Tree"
    }
   

$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,40)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)
.
.

$x = $hash[$textBox1.Text]
  • Related