I am implementing context-sensitive help for an existing WinForms app built in Visual Studio .NET. I have added a HelpProvider
to the form and set the HelpNamespace
property to a wonderful .chm
that covers every control and menu item on the form. I have set the necessary HelpKeyword
on all the controls that derive from Control
and so far all is great: F1 works perfectly.
My problem is that I can't work out how to do it for menu items. These use the ToolStripMenuItem
class, which does not derive from Control
and so has no HelpKeyword
property. How should I provide context-sensitive help for individual menu items? Mr. Google has not been very forthcoming.
Using F1 is not a common way of providing help for menu items. Menu items usually use ToolTip, or show some help text in StatusBar or usually their comprehensive helps comes with Help content of main page.
I prefer to use one of above mentioned solutions, but here for learning purpose, I'll show what you can do using
HelpRequested
event of the form.To handle help for form and controls, you can rely on the
HelpRequested
event of the form and controls.Here you can rely on
Form
event to solve the problem. Since you have aHelpProvider
on form, you should knowHelpProvider
handlesHelpRequested
event of all controls internally and, for controls havingShowHelp
set totrue
, it setsHandled
totrue
and prevents bubbling the event up so you can not have your custom code for handling help event ifShowHelp
istrue
. So you should setShowHelp
for controls tofalse
and just useHelpProvider
as a help key holder.To solve the problem using the
HelpRequested
event of the form, you should follow these steps:ToolStripMenuItems
, use theTag
property as the help key holder.HelpProvider
to assignHelpKey
, don't forget to setShowHelp
tofalse
.HelpRequested
event of the form.Tag
property of the active item to show help. If there is not any active menu, use theActiveControl
property of the form to show the help.Example
Here is a step by step example of how you can show help for menu items using F1 key. To do so, follow these steps:
Form
and put some controls and aMenuStrip
having some menu and sub menus on the form.HelpProvider
control on form and for each control assign suitable key toHelpKeyword
property of control. Also setShowHelp
for each control to false. We will handle help in code.ToolStripMenuItem
use itsTag
property to store the help keyword.Creating a helper method to find descendants of the Menu - Add a class to your application having the following code. In the following code, I've introduced an extension method to get all sub
ToolStripMenuItem
of aMenuStrip
:Handling the Helprequested event to show help - Handle the
HelpRequested
event of the form and implement the algorithm which I described above using the following code:Note
Text
property of form. It means the solution is working and after that you can create suitable chm file.ShowHelp
method ofHelp
class based on your requirement.HelpKeyword
andHelpString
extended properties for controls, pay attention which one you are using and get the same one in theHelpRequested
event.ShowHelp
to false. If you forget this step, the event will be handled internally inHelpprovider
.Tag
property of menu items. To make it more friendly for future, you can simply create an extender provider that adds a help keyword property to menu items.