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.
- 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.

- 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. |
- Select the form.
- In the properties window, change HelpNavigator to
Topic
- Set HelpKeyword to the topic page you want to be opened
from the form.
Now if you run your application and press F1, the help file opens with the
correct topic selected.
There are two help buttons here. The first is a button you placed on your
form yourself, the second the built-in 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.  |
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.
- Select the form
- In the Properties window, set HelpButton property to True and Maximize and Minimize
boxes False
- Add a control, e.g. a text box, to your form and set the HelpKeyword
property in the Properties window.
- In the Properties window of the control, set the
HelpNavigator property to Topic
- 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
- In the ToolBox double-click the MainMenu control.
- Click the first Type Here box and type "&Help".
- In the Type Here box directly below the first box, type "&Contents...",
name mnuContents
- Add two more menu items beneath Contents menu item: "&Index..." and
"&Search..."
- 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
|
- 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
|
- 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.