.netCoders Contact Us
Search:

Online Help

One of the standards in Windows applications is that pressing F1 launches a Help file associated with your application. Often, applications will provide context-sensitive help. This means that, when the Help is loaded, help content specific to the task at hand is loaded. This saves the user time, since they are not presented with a generic help homepage, and have to navigate to the correct topic themselves.

Fortunately, there is a control specifically designed for these tasks, called the HelpProvider. This control is used to associate an HTML Help file (*.chm or *.htm) with your application. By adding this control to your form, other form controls gain additional Help-related properties in design mode. (Note: When the HelpProvider is added to a form, it appears in the tray at the bottom of the Designer. It is not a UI element.) Using these properties, you can provide context-sensitive help down to the element level. You also can manipulate the HelpProvider object programmatically.

To set the Help file loaded by the HelpProvider object when the user presses F1, set the HelpNamespace property on the HelpProvider object. You can select a specific compiled HTML Help file that you provide with your application, or alternatively, you can indicate a URL. This is useful if you plan on providing help via a website.

//
// Set the HelpNamespace to a Help file (*.chm) or an HTML file, online or local.
//
helpProvider1.HelpNamespace = "http://help.dotnetcoders.com/form1/gettingstarted.htm";

The HelpRequested Event

If you want more control when a user presses F1 for help, you can handle the HelpRequested event for a control and write your own custom code. Here is an trivial example that acknowledges that help was requested:
this.textBox2.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.textBox2_HelpRequested);
...
private void textBox2_HelpRequested(object sender, System.Windows.Forms.HelpEventArgs hlpevent)
{
    MessageBox.Show("Help has been requested.");
}

Pop-up Help

One of the nice features of this control is that you do not need to create a full-fledged Help file to provide help. Using the HelpString property, you can add text to controls that shows up as a pop-up window (similiar to a tooltip) when the user hits F1, and the control has focus. In the Windows Form Designer, you can set this information in the properties window. This translates into method calls in the InitializeComponent method. If you want to do this programmatically, call the SetHelpString method on the HelpProvider.
//
// Call the SetHelpString method, passing in the control and the help text, to set pop-up help.
//
helpProvider1.SetHelpString(this.textBox1, "Enter your last name. If you are married, enter your married name.");
On a form, this would look like the following: