key preview

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
hi
i want to open the help file of my application whenever user press F1 in any of my 84 forms.
so should I enable the keypreview of all my forms and check if user press F1 in each form to show the help file?
because i heard that it's not recommended to enable the form key preview.
is there any other way?
thanks
 
Who says that it is not recommended to enable KeyPreview, and why? It isn't necessary in this case, though. It seems that the active control, its container, its container's container, etc., will all raise the HelpRequested event when the user presses F1 (this includes the active form).

I recommend that you create a static class that will handle all of the Forms' HelpRequested events, that way you can have a small amount of code in one place that handles this feature. You can either create a new Form class that does this and derive all 84 of your forms from that or you can add a statement to each form's constructor that adds that form's HelpRequested event to the class' HelpRequested event handler.

Beware, though, that this event seems to be raised more than once when the user presses F1. When you handle the event, be sure the set the HelpEventArgs.Handled property to true to prevent the event from being raised again.
 
thanks:)
but my help file is a little bit different as the others!
what if i want to show a simple messagebox when user press F1?
still possible to use your method?
 
You can do whatever you like when the user presses F1.

The important things you need to know are:
  • The HelpRequested event is raised when F1 is pressed.
  • You handle this event in any way you like.
  • Be sure to set the HelpEventArgs.Handled property to true if you handle the event.
  • The HelpRequested event is actually intended to be used for context-sensitive help and can also be raised by clicking the question mark button in the title bar if this button is displayed on your form, so make sure that you don't require the use of this feature if you choose this solution.
 
Back
Top