[英]Context-sensitive help for menu items in Windows Forms
I am implementing context-sensitive help for an existing WinForms app built in Visual Studio .NET.我正在为在 Visual Studio .NET 中构建的现有 WinForms 应用程序实现上下文相关帮助。 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.我在表单中添加了一个
HelpProvider
,并将HelpNamespace
属性设置为一个美妙的.chm
,它涵盖了表单上的每个控件和菜单项。 I have set the necessary HelpKeyword
on all the controls that derive from Control
and so far all is great: F1 works perfectly.我已经在从
Control
派生的所有控件上设置了必要的HelpKeyword
,到目前为止一切都很好:F1 完美运行。
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.它们使用
ToolStripMenuItem
类,该类不是从Control
派生的,因此没有HelpKeyword
属性。 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.使用F1不是为菜单项提供帮助的常用方法。 Menu items usually use ToolTip , or show some help text in StatusBar or usually their comprehensive helps comes with Help content of main page .
菜单项通常使用ToolTip ,或者在StatusBar 中显示一些帮助文本,或者它们的综合帮助通常带有主页的帮助内容。
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.我更喜欢使用上述解决方案之一,但出于学习目的,我将展示您可以使用表单的
HelpRequested
事件执行的操作。
To handle help for form and controls, you can rely on the HelpRequested
event of the form and controls.要处理窗体和控件的帮助,您可以依赖窗体和控件的
HelpRequested
事件。
Here you can rely on Form
event to solve the problem.这里可以依靠
Form
事件来解决问题。 Since you have a HelpProvider
on form, you should know HelpProvider
handles HelpRequested
event of all controls internally and, for controls having ShowHelp
set to true
, it sets Handled
to true
and prevents bubbling the event up so you can not have your custom code for handling help event if ShowHelp
is true
.由于您在表单上有一个
HelpProvider
,您应该知道HelpProvider
在内部处理所有控件的HelpRequested
事件,对于将ShowHelp
设置为true
控件,它将Handled
设置为true
并防止事件冒泡,因此您无法使用自定义代码进行处理如果ShowHelp
为true
则帮助事件。 So you should set ShowHelp
for controls to false
and just use HelpProvider
as a help key holder.因此,您应该将控件的
ShowHelp
设置为false
并将HelpProvider
用作帮助密钥持有者。
To solve the problem using the HelpRequested
event of the form, you should follow these steps:要使用表单的
HelpRequested
事件解决问题,您应该按照以下步骤操作:
ToolStripMenuItems
, use the Tag
property as the help key holder.ToolStripMenuItems
,使用Tag
属性作为帮助键持有者。HelpProvider
to assign HelpKey
, don't forget to set ShowHelp
to false
.HelpProvider
分配HelpKey
,请不要忘记将ShowHelp
设置为false
。HelpRequested
event of the form.HelpRequested
事件。Tag
property of the active item to show help.Tag
属性显示帮助。 If there is not any active menu, use the ActiveControl
property of the form to show the help.ActiveControl
属性来显示帮助。 Example示例
Here is a step by step example of how you can show help for menu items using F1 key.以下是如何使用F1键显示菜单项帮助的分步示例。 To do so, follow these steps:
为此,请按照下列步骤操作:
Form
and put some controls and a MenuStrip
having some menu and sub menus on the form.Form
并在Form
放置一些控件和一个带有一些菜单和子菜单的MenuStrip
。HelpProvider
control on form and for each control assign suitable key to HelpKeyword
property of control.HelpProvider
控件,并为每个控件为控件的HelpKeyword
属性分配合适的键。 Also set ShowHelp
for each control to false. ShowHelp
每个控件的ShowHelp
设置为 false。 We will handle help in code.ToolStripMenuItem
use its Tag
property to store the help keyword.ToolStripMenuItem
使用其Tag
属性来存储帮助关键字。 Creating a helper method to find descendants of the Menu - Add a class to your application having the following code.创建一个辅助方法来查找 Menu 的后代- 将一个类添加到您的应用程序中,该类具有以下代码。 In the following code, I've introduced an extension method to get all sub
ToolStripMenuItem
of a MenuStrip
:在下面的代码中,我引入了一个扩展方法来获取
MenuStrip
所有子ToolStripMenuItem
:
using System.Collections.Generic; using System.Linq; using System.Windows.Forms; public static class ToolStripMenuItemExtensions { public static List<ToolStripMenuItem> Descendants(this MenuStrip menu) { var items = menu.Items.OfType<ToolStripMenuItem>().ToList(); return items.SelectMany(x => Descendants(x)).Concat(items).ToList(); } public static List<ToolStripMenuItem> Descendants(this ToolStripMenuItem item) { var items = item.DropDownItems.OfType<ToolStripMenuItem>().ToList(); return items.SelectMany(x => Descendants(x)).Concat(items).ToList(); } }
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:处理 Helprequested 事件以显示帮助- 处理表单的
HelpRequested
事件并使用以下代码实现我上面描述的算法:
private void Form1_HelpRequested(object sender, HelpEventArgs hlpevent) { string keyword = ""; var selectedMenuItem = this.menuStrip1.Descendants() .Where(x => x.Selected).FirstOrDefault(); if (selectedMenuItem != null) keyword = selectedMenuItem.Tag?.ToString(); else if (ActiveControl != null) keyword = helpProvider1.GetHelpKeyword(ActiveControl); if (!string.IsNullOrEmpty(keyword)) Help.ShowHelp(this, "Help.chm", HelpNavigator.Index, keyword); }
Note注意
Text
property of form.Text
属性中显示 helpkeyword。 It means the solution is working and after that you can create suitable chm file.ShowHelp
method of Help
class based on your requirement.Help
类的ShowHelp
方法的其他重载之一。HelpKeyword
and HelpString
extended properties for controls, pay attention which one you are using and get the same one in the HelpRequested
event.HelpKeyword
和HelpString
扩展属性,注意您使用的是哪一个,并在HelpRequested
事件中获取相同的属性。ShowHelp
to false.ShowHelp
设置为 false。 If you forget this step, the event will be handled internally in Helpprovider
.Helpprovider
内部处理。Tag
property of menu items.Tag
属性分配帮助关键字。 To make it more friendly for future, you can simply create an extender provider that adds a help keyword property to menu items.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.