如何设计可重用的上下文菜单? [英] How to design reusable context menus?

查看:36
本文介绍了如何设计可重用的上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发WinForms .NET应用程序.我只有几个上下文菜单,这些菜单可在各种地方使用,包括一些用户控件.没有最终的根窗体可以将这些菜单(我可以在这些窗体的设计器中进行设计)保存在一个地方.

I'm developing a WinForms .NET application. I have few context menus which are used on various places including some user controls. There is no ultimate root form which could hold those menus (I could design in those form's designer) on a single place.

我想使这些菜单成为独立的控件,并能够在需要的地方从工具箱中删除它们.我知道如何以编程方式/动态方式执行此操作,但没有设计人员的支持.也许我只是想念一些东西.

I would like to make those menus standalone controls and be able to drop them from Toolbox wherever I need. I know how to do it programmatically/dynamically but without designer support. Perhaps I'm just missing something.

  • 有没有办法使这些菜单可设计?
  • 最佳做法是什么?

推荐答案

您可以创建一个可重用的自定义上下文菜单,该菜单可在工具箱中使用.为此,在从 System.Windows.Forms.ContextMenuStrip 继承的项目中创建一个公共类就足够了,并将用于创建项目的代码放在您的类的构造函数中.

You can create a reusable custom context menu which is available in toolbox. To do so, it's enough to create a public class in the project which inherits from System.Windows.Forms.ContextMenuStrip and put the codes for creating items in constructor of your class.

您也可以使用设计器来做到这一点:

Also you can do it using designer:

  • 将新类 CustomContextMenuStrip 添加到您的项目
  • 将其公开,并从 System.Windows.Forms.ContextMenuStrip 继承并保存.
  • 双击文件在设计模式下将其打开,其中显示空白区域,并显示一条消息,提示您可以添加一些组件.
  • 在属性窗口中,单击 Items 属性,然后添加所需的项目.
  • 它将在 InitializeComponent()方法中为项目创建代码.转到文件的代码视图,并添加一个公共的无参数构造函数,然后在其中调用 InitializeComponent().
  • 如果您构建项目,则自定义组件将添加到工具箱顶部项目的组件"标签中的工具箱中.
  • Add a new class CustomContextMenuStrip to your project
  • Make it public and inherit from System.Windows.Forms.ContextMenuStrip and save it.
  • Double click on file to open it in design mode which shows an empty area with a message which tells you can add some components.
  • In property window, click Items property and add items that you need.
  • It will creates the codes for items in InitializeComponent() method. Go to code view of the file and add a public parameterless constructor and call InitializeComponent() in it.
  • If you build the project, your custom component will add to toolbox in your project Components tab at top of toolbox.

注意:

  • 使用设计器方法具有使用设计器的好处,例如,使用设计器,您可以通过设置仅在设计器中可用的 Language 属性来使组件可本地化.这样,您的组件本身就可以本地化,而与主机形式无关.
  • 对于所有可继承的组件和控件,您都可以采用这种方法,而不仅限于 ContextMenuStrip .
  • 您可以通过在自定义类中添加事件处理程序来向菜单项添加行为.另外,如果需要让托管组件的表单添加事件处理程序,则可以在表单中处理 ItemClicked .
  • 虽然您可以在宿主表单的设计器中看到这些项目,但是不能使用宿主表单的设计器进行更改.
  • Using the designer approach has benefits of using designer, for example using the designer, you can make your component localizable by setting Language property which is available only in designer. This way your component itself is localizable, independent from host form.
  • You can follow this approach for all components and controls which are inheritable and it's not restricted to ContextMenuStrip.
  • You can add behavior to menu items by adding event handlers in your custom class. Also if you need to let the forms that host your component add event handlers, you can hanlde ItemClicked in forms.
  • Whilst you can see those items in designer of host forms, but you can not change them using designer of the host forms.

这篇关于如何设计可重用的上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆