Typing Enter Pushes a Button

August 17th, 2015 by June Blender
Last updated on September 17th, 2015

 

As a beginner developer of PowerShell GUIs, I’m always looking for easy ways to make my simple applications more sophisticated and usable. One of the basic usability features in most applications is typing <Enter> in a textbox to invoke a function instead of clicking a Start, Go, or OK button.

In this post, I’ll show you two ways to do it. One technique is significantly simpler than the other, but works only when there is exactly one “Go” button. The more complex technique works for multiple buttons and keys other than <Enter>.

Registering a KeyDown Event Handler

Let’s start with the general, more complex technique. We’ll create a KeyDown event handler for the <Enter> key, but you can use the same method to register an event handler for any key. Our event handler will click a particular button, but you can set it up to click any button on the user’s behalf.

Here’s the typical situation. In this little app, when you type a valid value in the textbox, the Get-Service button is enabled. You can click the Get-Service button, but it would be nice to let the user just type <Enter> after typing the computer name.

GetServicePsf

To enable the “press <Enter>” feature, use the KeyDown event of the Textbox object (System.Windows.Forms.Textbox):

1. Start by creating a variable to hold the KeyDown event of the textbox. Following the best practice naming guidelines, I’ll name the variable $textboxComputerName_KeyDown.

$textboxComputerName_KeyDown = {}

2. The KeyDown event handler requires a KeyEventHandler object, so we’ll cast the script block as a KeyEventHandler object.

$textboxComputerName_KeyDown = [System.Windows.Forms.KeyEventHandler]{}

3. When the KeyCode property value ($_.KeyCode) is “Enter”, we want to click the Get-Service button on the user’s behalf. To click the button, call the Invoke() method of the Click event of the button ($buttonGetService.Click). Referencing the Click event without calling the Invoke() method has no effect.

$textboxComputerName_KeyDown = [System.Windows.Forms.KeyEventHandler]{
    if ($_.KeyCode -eq 'Enter')
    {
        $buttonGetServices_Click.Invoke()
    }
}

Or, you can use the PerformClick() method of the button. This makes your intent much clearer than just invoking an event handler function. (Many thanks to Charlie Eberly for this suggestion!)

$textboxComputerName_KeyDown = [System.Windows.Forms.KeyEventHandler]{
    if ($_.KeyCode -eq 'Enter')
    {
        $buttonGetServices.PerformClick()
    }
}

4. Set the SuppressKeyPress property of the KeyEventArgs object to $True.

When you invoke the Click event of the button on the user’s behalf, you want to prevent the user from clicking the button, too. Otherwise, the button could be clicked multiple times. Also, the Click generates a sound that’s typically associated with clicking a button, but not with pressing <Enter>. Setting SuppressKeyPress to $True takes care of this for you.

$textboxComputerName_KeyDown = [System.Windows.Forms.KeyEventHandler]{
 
    if ($_.KeyCode -eq 'Enter')
    {
        $buttonGetServices_Click.Invoke()
 
        #Suppress sound from unexpected use of enter on keyPress/keyUp
        $_.SuppressKeyPress = $true
    }
}

The KeyDown event handler is now complete.

The final step is to register the KeyDown event handler, that is, to associate the event hander with the KeyDown event so that when the KeyDown event happens (“is raised”), the system runs the event handler code. Because KeyDown is not the default event of a Textbox object, PowerShell Studio doesn’t do it for you, but it’s very easy to do.

To register the event handler in PowerShell Studio:

1. In PowerShell Studio, in the Designer window, click the textbox.

2. In the Properties pane for the textbox, on the row for the KeyDown event, type the name of the event handler.

clip_image004

To register the event manually (without PowerShell Studio):

1. Write a statement that registers the event handler.

The syntax is:

<FormControl>.add<EventName>()

For example:

$textboxComputerName.add_KeyDown($textboxComputerName_KeyDown)

2. Remember to remove the event handler in your cleanup step.

For example:

$textboxComputerName.remove_KeyDown($textboxComputerName_KeyDown)

Set the Accept Button of the Form

Writing a KeyDown event is conceptually a bit complex, but it’s not difficult to do. However, the other technique is so simple that you’ll use it whenever you can.

Every form has an AcceptButton property that is invoked automatically when the user types <Enter>. Typically, the AcceptButton property is set to the OK button, if there is one, but you can set it to any button on the form (or not set it).

NOTE: In PowerShell Studio, when you use a form template that includes an OK button, such as a Dialog Style form, the AcceptButton is set to the OK button by default.

In the PowerShell Studio designer and in the running application, the AcceptButton is indicated by a blue border.

clip_image006

To set the AcceptButton property of the form:
— In the Properties box for the form, in the row for the AcceptButton property, use the drop-down menu to select a button.

clip_image007

-or-

— In the Load event of the form, set the AcceptButton property of the form to the desired button.

$formGetService_Load = {
    $formGetService.AcceptButton = $buttonGetService
}

 

Now that you know both techniques, remember not to use them together. If the AcceptButton property of the form is set to any button, that property value takes precedence over a KeyDown event with an ‘Enter’ key code. For example, if you write a KeyDown event for $buttonGetServices and set the AcceptButton property of the form is $buttonOK, when you click enter in the textbox, it clicks $buttonOK.

Allowing the user to press <Enter> in your app, instead of forcing them to click a button, improves the usability of your app. Spend an extra few minutes setting the AcceptButton property, or even registering a KeyDown event.

June Blender is a technology evangelist at SAPIEN Technologies, Inc and a Windows PowerShell MVP. You can reach her at juneb@sapien.com and follow her on Twitter at @juneb_get_help.

 

 
[Google+]   [Facebook]   [LinkedIn]   [StumbleUpon]   [Digg]   [Reddit]   [Google Bookmark]  

Tags: , , ,