简体   繁体   English

Windows 窗体中菜单项的上下文相关帮助

[英]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并防止事件冒泡,因此您无法使用自定义代码进行处理如果ShowHelptrue则帮助事件。 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事件解决问题,您应该按照以下步骤操作:

  1. For ToolStripMenuItems , use the Tag property as the help key holder.对于ToolStripMenuItems ,使用Tag属性作为帮助键持有者。
  2. For other controls, if you use HelpProvider to assign HelpKey , don't forget to set ShowHelp to false .对于其他控件,如果您使用HelpProvider分配HelpKey ,请不要忘记将ShowHelp设置为false
  3. Handle the HelpRequested event of the form.处理表单的HelpRequested事件。
  4. In the body of event handler, check if there is an active menu item on your form, then use the 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:为此,请按照下列步骤操作:

  1. Create Form, Menu and Controls - Create a Form and put some controls and a MenuStrip having some menu and sub menus on the form.创建窗体、菜单和控件- 创建一个Form并在Form放置一些控件和一个带有一些菜单和子菜单的MenuStrip
  2. Configuring HelpProvider - Put a HelpProvider control on form and for each control assign suitable key to HelpKeyword property of control.配置 HelpProvider - 在表单上放置一个HelpProvider控件,并为每个控件为控件的HelpKeyword属性分配合适的键。 Also set ShowHelp for each control to false. ShowHelp每个控件的ShowHelp设置为 false。 We will handle help in code.我们将处理代码方面的帮助。
  3. Configuring Help for Menu - For a ToolStripMenuItem use its Tag property to store the help keyword.为菜单配置帮助- 对于ToolStripMenuItem使用其Tag属性来存储帮助关键字。
  4. 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(); } }
  5. 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注意

  • For testing the solution you don't need a chm file having index and so on.为了测试解决方案,您不需要具有索引等的 chm 文件。 You can simply show the helpkeyword in Text property of form.您可以简单地在表单的Text属性中显示 helpkeyword。 It means the solution is working and after that you can create suitable chm file.这意味着解决方案正在运行,之后您可以创建合适的 chm 文件。
  • You can use one of the other overloads of ShowHelp method of Help class based on your requirement.您可以根据需要使用Help类的ShowHelp方法的其他重载之一。
  • There are HelpKeyword and HelpString extended properties for controls, pay attention which one you are using and get the same one in the HelpRequested event.控件有HelpKeywordHelpString扩展属性,注意您使用的是哪一个,并在HelpRequested事件中获取相同的属性。
  • Don't forget to set ShowHelp to false.不要忘记将ShowHelp设置为 false。 If you forget this step, the event will be handled internally in Helpprovider .如果您忘记了这一步,该事件将在Helpprovider内部处理。
  • Don't forget to assign a help keyword to 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM