Microsoft KB Archive/821777

= How to provide context-sensitive help or online help in a Windows application by using Visual Basic =

Article ID: 821777

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





SUMMARY
This step-by-step article describes how to provide online help pages and context-sensitive help for the controls in a Microsoft Windows application. The example uses the HelpProvider class and the Help class to provide help for the controls in the application.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET
 * Internet Connectivity

This article assumes that you are familiar with the following topics:
 * Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET programming

About the HelpProvider class
The HelpProvider class provides context-sensitive help or online help pages for controls. Each instance of HelpProvider maintains a collection of references to the controls that are associated with the instance. To provide online help pages for the control, set the HelpNamespace property of HelpProvider, and then associate a Help file with the HelpProvider object.

You can specify the type of help that the application provides for the control by calling the SetHelpNavigator method, and by providing a HelpNavigator value for the specified control. Help and HelpProvider use the HelpNavigator enumeration to provide access to specified elements of the Help file. The members of HelpNavigator are the following:
 * AssociateIndex
 * Find
 * Index
 * KeywordIndex
 * TableOfContents
 * Topic

You provide the keyword or topic for the Help file by calling the SetHelpKeyword method. You use the SetHelpString method of HelpProvider to associate a specific Help string with a control. The string that you associate with a control by using the SetHelpString method appears in a pop-up window when you press F1 while the control has focus.

When HelpNamespace is not set, use SetHelpString to provide the help text. When you set both HelpNamespace and the Help string, the help that is based on the HelpNamespace takes precedence.

About the Help class
The Help class provides help to an application by calling the static ShowHelp method and the static ShowHelpIndex method. The ShowHelp method displays the contents of a Help file. The ShowHelpIndex method displays the index of the specified Help file. You can use the Help object to display Help files such as Compiled Help Module (.chm) files or HTML files that are in the HTML Help format.

Use the HelpProvider class to provide online help for controls
The following example describes how to use the HelpProvider class to display context-sensitive Help file content for a form that contains controls.

Set the HelpNamespace property of the HelpProvider class to an HTML file or to a .chm file. To do this, follow these steps:
 * 1) Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Under Project Types, click Visual Basic Projects. Under Templates, click Windows Application, and then click OK. By default, a form that is named Form1 is created.

Note In Visual Studio 2005, click Visual Basic under Project Types.
 * 1) Add a HelpProvider component to the Form1 form.
 * 2) Right-click HelpProvider1, and then click Properties.
 * 3) In the Properties dialog box, set the HelpNamespace property to http://msdn.microsoft.com/library/en-us/vbcon/html/vbconbuttoncontroloverview.asp.
 * 4) Add a Button control to the Form1 form. Button1 is created.
 * 5) In the Properties dialog box of Button1, set ShowHelp on HelpProvider1 to True.
 * 6) Set the Text property of Button1 to Submit.

Use the HelpProvider class to provide context-sensitive help for controls
The following example describes how to use the HelpProvider class to display help for the controls in a pop-up window. Use the SetHelpString method of the HelpProvider class to set the Help ToolTip text for the controls. When you click the Help button on the title bar of the Form1 form, and then click the control, the Help string appears.  Add two TextBox controls to the Form1 form.  Add the following sample code to the Form1_Load event handler. 'Set the Help string for the TextBox control on the form. HelpProvider1.SetHelpString(Me.TextBox1, &quot;Enter your UserName&quot;) HelpProvider1.SetHelpString(Me.TextBox2, &quot;Enter your Password in this TextBox&quot;) 'Set the Help string for the Button control on the form. HelpProvider1.SetHelpString(Me.Button1, &quot;Click submit after you type your UserName and Password&quot;)  Right-click Form1, and then click Properties. In the Properties dialog box, set HelpButton to True. Set MaximizeBox to False. Set MinimizeBox to False.

Use the SetHelpNavigator method to provide a specified type of help
The following example describes how to use the SetHelpNavigator method to retrieve the table of contents from the Notepad.chm Help file.  Add a HelpProvider component to the Form1 form.</li> Right-click HelpProvider2, and then click Properties.</li> In the Properties dialog box, set the HelpNamespace property of HelpProvider2 to the path of the Notepad.chm file that is on your computer. For example, set the path to C:\WINDOWS\Help\Notepad.chm.</li> Add a Button control to the Form1 form.</li> In the Properties dialog box, set the Text property of Button2 to Save UserName in Notepad.</li> Set the ShowHelp on HelpProvider2 property of Button2 to True.</li>  Add the following statement at the top of the code in the Form1 form. Imports System.IO </li>  Add the following sample code to the Button2_Click event handler. Dim path As String = &quot;c:\temp\MyTest.txt&quot; Dim sw As StreamWriter ' This text is added to the file only one time. If File.Exists(path) = False Then ' Create a file to write to. sw = File.CreateText(path) 'Write the UserName and the password to the text file. sw.WriteLine(TextBox1.Text) sw.WriteLine(TextBox2.Text) sw.Flush sw.Close End If </li>  Add the following sample code to the Form1_Load event handler. 'Display the Table of Contents of the Help file when you press F1. HelpProvider2.SetHelpNavigator(Button2, HelpNavigator.TableOfContents) </li></ol>

Use the Help class to display an online Help file for a control
The following example describes how to use the ShowHelp method of the Help class to display the Help file for the ListBox control. If the control has focus, the Help file appears when you press F1.  Add a ListBox control to the Form1 form.</li> Right-click ListBox1, and then click Properties.</li> In the Properties dialog box, click the Items property, and then click the ellipsis button (...).</li> In the String Collection Editor dialog box, type the following strings: <ul>  Domain1 </li> <li> Domain2 </li> <li> Domain3 </li></ul> </li> <li>Click OK.</li> <li> Add the following sample code to the ListBox1_KeyDown event handler. If e.KeyCode = Keys.F1 Then 'Display the Help file for the ListBox control when you press F1. Help.ShowHelp(ListBox1, &quot;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vboriListBoxControlProgramming.asp&quot;) End If </li></ol>

Verify that it works
<ol> <li>On the Build menu, click Build Solution.</li> <li>On the Debug menu, click Start.</li> <li>Click the Help (?) button on the toolbar of the Form1 form, and then click any of the following controls: <ul> <li>TextBox1</li> <li>TextBox2</li> <li>Submit</li></ul>

The context-sensitive help for the control appears.</li> <li>When the Submit button has focus, press F1. The Help file for the Submit control appears.</li> <li>When the ListBox1 control has focus, press F1. The Help file for the ListBox1 control appears.</li> <li>When the Save UserName in Notepad button has focus, press F1. The Help file for Notepad appears.</li></ol>

<div class="references_section">