Dynamic Help Topics in C#

I’ve got a situation where I want to use a keyboard shortcut for a specific help topic: Keyboard Shortcuts. Oh, the irony… So I fart around trying to bang things into shape, but it’s really just not taking very well as help is really setup to use with controls, and not really setup to use dynamically/programmatically.

However, with a little hacking around, it’s not very difficult.

The following shows how to use a HelpProvider to dynamically choose a help topic without using a control. You need a Windows Form with a HelpProvider and a CHM file. You must know the CHM file and must use the proper keywords from it. In this case, the keywords is “Keyboard_Shortcuts.htm”. Note that it is NOT “Keyboard Shortcuts” or some other variation.

Here’s how to display a specific help topic from a keyboard shortcut in the KeyDown event handler:

case Keys.K:
    // do shortcuts here

    helpProvider1.SetHelpNavigator(this, HelpNavigator.Topic);
    helpProvider1.SetHelpKeyword(this, "Keyboard_Shortcuts.htm");
    this.Focus();
    SendKeys.SendWait("{F1}");
    helpProvider1.SetHelpNavigator(this, HelpNavigator.TableOfContents);

    break;

The things to watch there are that you:

  • Use the form for the help control (this),
  • Use “*.htm” for the keyword,
  • Set focus to your form (just in case),
  • Use the SendWait method to give the application time to respond to the key event, and
  • Reset the help back to the table of contents afterwards so that subsequent help requests don’t end up at the keyboard shortcuts (or whatever you use)

The last 2 there are the most important. If you don’t use the SendWait method, the following line where you reset the help provider will run before the key event has time to execute.

It’s very straight forward, but there are a few things to watch there.

I hope that saves someone a bit of time.

Cheers,

Ryan

Share

Written by:

275 Posts

View All Posts
Follow Me :

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.