Activating Help in Visual Studio.NET WinForms

Through the use of the HelpProvider component, you can easily attach Help topics within a Help file (that is either of HTML or HTMLHelp 1.x or greater format) to specific controls on Windows Forms.

Tip
If you use the Help Generator for Visual Studio.Net add-in, the pages in your help will automatically be connected to the generated pages in your HTML Help file (.chm).
Overview

Provide Help using F1

  1. Drag a HelpProvider component from the Toolbox to your form.
    The component will sit in the tray at the bottom of the Windows Forms Designer.
  2. In the Properties window, set the HelpNamespace property to the .chm or .htm Help file.


    Note
    With the filename without path we assume that your help file is located in the same folder as your application. So when you're developing, you will keep a copy of the help file in the \debug folder.
  3. Select the form.
  4. In the properties window, change HelpNavigator to Topic

      
  5. Set HelpKeyword to the topic page you want to be opened from the form.
    Note
    You can keep the HelpKeyword, assuming you have set the help project's Default Topic.

Now if you run your application and press F1, the help file opens with the correct topic selected.

Add a Help button

There are two help buttons here. The first is a button you placed on your form yourself, the second the built-in help button.

Custom Help button

To implement a custom Help button, drop a button on your form and add code like below to the click event.

c#
private void cmdHelp_Click(object sender, EventArgs e)
{
    System.Windows.Forms.Help.ShowHelp(this, helpProvider1.HelpNamespace, HelpNavigator.Topic, this.helpProvider1.GetHelpKeyword(this));
}
VB.NET
Private Sub cmdHelp_Click(object sender, EventArgs e)
    System.Windows.Forms.Help.ShowHelp(Me, helpProvider1.HelpNamespace, HelpNavigator.Topic, Me.helpProvider1.GetHelpKeyword(Me));
End Sub 
Note
In the above code GetHelpKeyword(this) is used so that you don't have to specify the topic page assuming it was already set on the form (= this) Alternatively, you could as the last argument just have put "MyTopic.htm". 
Tip
You can add an image to the button on the Image property. We added an icon which you can use in the \icons sub folder.

Built-in help button

You can enable the Built-in help button  for your form by setting its boolean HelpButton property to True. The catch in using this button in your forms is that you have to do away with the Maximize and Minimize boxes in the title bar.

  1. Select the form
  2. In the Properties window, set HelpButton property to True and Maximize and Minimize boxes False
  3. Add a control, e.g. a text box, to your form and set the HelpKeyword property in the Properties window.
  4. In the Properties window of the control, set the HelpNavigator property to Topic
  5. Set HelpKeyword to the topic page you want to be opened from the control

Open a help file set to the Contents, Index, or Search tab

  1. In the ToolBox double-click the MainMenu control.
  2. Click the first Type Here box and type "&Help".
  3. In the Type Here box directly below the first box, type "&Contents...", name mnuContents
  4. Add two more menu items beneath Contents menu item: "&Index..." and "&Search..."
  5. Double-click the Contents menu item and then add the following code to display the .chm file set to the Contents Tab:
    c#
    private void mnuContents_Click(object sender, EventArgs e)
    {
    System.Windows.Forms.Help.ShowHelp(this, helpProvider1.HelpNamespace);
    }
    VB.NET
    Private Sub mnuContents_Click(object sender, EventArgs e)
        System.Windows.Forms.Help.ShowHelp(Me, helpProvider1.HelpNamespace);
    End Sub
  6. To display the .chm file set to the Index tab, double-click the Index menu item and then add the following code:

    c#
    private void mnuIndex_Click(object sender, EventArgs e)
    {
    System.Windows.Forms.Help.ShowHelpIndex(this, helpProvider1.HelpNamespace);
    }
    VB.NET
    Private Sub mnuIndex_Click(object sender, EventArgs e)
    System.Windows.Forms.Help.ShowHelpIndex(Me, helpProvider1.HelpNamespace);
    End Sub
  7. To display the .chm file set to the Search tab, double-click the Search menu item and then add the following code:

    c#
    private void mnuSearch_Click(object sender, EventArgs e)
    {
    System.Windows.Forms.Help.ShowHelpIndex(this, helpProvider1.HelpNamespace, helpNavigator.Find, "");
    }
    Press F5 to Start. When the application displays the form, click the Help menu's items to see them working.
Activating help from your application