PrimalForms/PowerShell – Event Behavior: Tab Key or Enter Key

In this article I wanted to expand on the previous post (http://itnotes.net/2011/01/03/primalformspowershell-event-behavior-enter-key-and-active-directory-lookup/) …

Which captured the Event Behavior: KeyPress which was used to trigger an action after the Enter Key was pressed.

During a project to allow our admins to change the manager field in our AD LDS Identity Store I was coding a Textbox to be triggered with the tab key, but low and behold they TAB key is not captured by default by the KeyDown, KeyPress or KeyUp Events.

Per the Microsoft Articles:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup.aspx

Certain keys, such as the TAB, RETURN, ESC, and arrow keys are handled by controls automatically. To have these keys raise the KeyDown event, you must override the IsInputKey method in each control on your form. The code for the override of the IsInputKey would need to determine if one of the special keys is pressed and return a value of true. Instead of overriding the IsInputKey method, you can handle the PreviewKeyDown event and set the IsInputKey property to true.

Note the IsInputKey method must be set per control, so there is no need to turn it back off for other controls on the same form.

So in this example I start with a basic form with two labels, two textboxes, and a status bar to show when the TAB or ENTER keys are captured.

For the Event Behaivior: PreviewKeyDown for $textbox1 I setup the following code:

$textbox1_PreviewKeyDown=[System.Windows.Forms.PreviewKeyDownEventHandler]{
     #Event Argument: $_ = [System.Windows.Forms.PreviewKeyDownEventArgs]
     $_.IsInputKey = $true
}

For the Event Behaivor: KeyDown for $textbox1 I setup the following code:

$textbox1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
      If ($_.KeyValue -eq 9) {
           $statusbar1.Text = “Tab key was pressed”
           $textbox2.Focus()
           $textbox2.Clear()
     }
     Else {$statusbar1.Text = $_.KeyValue}
}

Notes:

     1. I used the KeyDown event because the KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

     2. I use $textbox2.Focus() to move to the next control because the TAB key is no longer coded to complete that default action.

Set $textbox1 Events to the previous code sets.

To show the different between the two examples I coded $textbox2 Event Behavior: KeyPress to capture the ENTER key

$textbox2_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
      If ($_.KeyChar -eq 13) {
           $statusbar1.Text = “Enter Key was pressed”
           $textbox1.Focus()
           $textbox1.Clear()
     }
}

Set $textbox2 to the previous code set.

As you may have noticed in the Event to capture the TAB key we used KeyValue and in the Event to capture the ENTER key we used KeyChar.

KeyCode, KeyData or KeyValue must be used because KeyChar cannot capture the following Keys

The TAB key.

INSERT and DELETE.

HOME.

END.

PAGE UP and PAGE DOWN.

F1-F2.

ALT.

Arrow keys.

For more details about the differences between KeyCode, KeyData, and KeyValue:

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keycode.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keydata.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.keyvalue.aspx

In my next post I would like to show examples of how to use KeyChar to to control the field format of a textbox used for items like ZipCode or Telephone Numbers.

~ITNotes

Leave a Reply

Your email address will not be published. Required fields are marked *