powershelleventhandler

PowerShell call function on enter in a textbox


I'm trying to validate a user input made in a textbox. But instead of always adding a button that he has to press i would like to check it automatically when the user hits the "Enter"-Key.

So my question is: Can PowerShell call a function as soon as the user hits the Enter in a specific textbox?


Solution

  • Simple example in Windows Forms:

    Add-Type -AssemblyName System.Windows.Forms
    
    $form = New-Object System.Windows.Forms.Form
    
    $textbox = New-Object System.Windows.Forms.TextBox
    
    $textbox.Add_KeyDown({
        if ($_.KeyCode -eq [System.Windows.Forms.Keys]::Enter) {
            #logic
            $textbox.Text | Out-Host
        }
    })
    
    $form.Controls.Add($textbox)
    
    $form.ShowDialog()