Search code examples
formswinformspowershelluser-interfacepowershell-5.0

Pressing <Enter> while cursor focus is in a text box should activate a specific button on the form. [Powershell]


Image of Form

When a user enters text into one of the search boxes, I would like them to be able to hit Enter to perform the same function as clicking a button would.

For example if they were typing in the boxed labeled "Find Username" - they would have the option to either press Enter or click the "Show Users" button. If they were typing in the box labeled "Find First Group" then pressing Enter would then be the equivalent of selecting the "Show 1st Groups" button.

Does anyone know how I can achieve this?


Solution

  • Put in a KeyEventHandler to the TextBox

    $textBox1.add_KeyDown($textbox1_KeyDown)
    $textbox1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
        if($_.KeyCode -eq 'Enter')
        {
            ...
        }
    }
    

    EDIT Forgot to add the code for associating the event handler to the object.