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
HelpRequestedevent of the form.To handle help for form and controls, you can rely on the
HelpRequestedevent of the form and controls.Here you can rely on
Formevent to solve the problem. Since you have aHelpProvideron form, you should knowHelpProviderhandlesHelpRequestedevent of all controls internally and, for controls havingShowHelpset totrue, it setsHandledtotrueand prevents bubbling the event up so you can not have your custom code for handling help event ifShowHelpistrue. So you should setShowHelpfor controls tofalseand just useHelpProvideras a help key holder.To solve the problem using the
HelpRequestedevent of the form, you should follow these steps:ToolStripMenuItems, use theTagproperty as the help key holder.HelpProviderto assignHelpKey, don't forget to setShowHelptofalse.HelpRequestedevent of the form.Tagproperty of the active item to show help. If there is not any active menu, use theActiveControlproperty 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:
Formand put some controls and aMenuStriphaving some menu and sub menus on the form.HelpProvidercontrol on form and for each control assign suitable key toHelpKeywordproperty of control. Also setShowHelpfor each control to false. We will handle help in code.ToolStripMenuItemuse itsTagproperty 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
ToolStripMenuItemof aMenuStrip:Handling the Helprequested event to show help - Handle the
HelpRequestedevent of the form and implement the algorithm which I described above using the following code:Note
Textproperty of form. It means the solution is working and after that you can create suitable chm file.ShowHelpmethod ofHelpclass based on your requirement.HelpKeywordandHelpStringextended properties for controls, pay attention which one you are using and get the same one in theHelpRequestedevent.ShowHelpto false. If you forget this step, the event will be handled internally inHelpprovider.Tagproperty 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.