NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Form.KeyPreview

Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.

[Visual Basic]
Public Property KeyPreview As Boolean
[C#]
public bool KeyPreview {get; set;}
[C++]
public: __property bool get_KeyPreview();
public: __property void set_KeyPreview(bool);
[JScript]
public function get KeyPreview() : Boolean;
public function set KeyPreview(Boolean);

Property Value

true if the form will recieve all key events; false if the currently selected control on the form receives key events. The default is false.

Remarks

When this property is set to true, the form will receive all System.WinForms.Control.KeyPress, System.WinForms.Control.KeyDown, and System.WinForms.Control.KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event-handling methods of the form the TextBox control will receive the key that was pressed. To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the e.Handled parameter in your form's KeyPress or KeyDown event to true.

You can use this property to process all keystrokes in your application and either handle the keystroke or call the appropriate control to handle the keypress. For example, when an application uses function keys, you may want to process the keystrokes at the form level rather than writing code for each control that might receive keystroke events.

Note   If a form has no visible or enabled controls, it automatically receives all keyboard events.

See Also

Form Class | Form Members | System.WinForms Namespace | System.WinForms.Control.KeyPress | System.WinForms.Control.KeyDown | System.WinForms.Control.KeyUp