如何以通用方式启用/禁用剪切/复制/粘贴菜单和工具栏项? [英] How do I enable/disable Cut/Copy/Paste menu and toolbar items in a generic way?

查看:34
本文介绍了如何以通用方式启用/禁用剪切/复制/粘贴菜单和工具栏项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 windows 窗体应用程序,其中包含文本框、组合框、datagridview 等控件.这些控件允许用户使用剪贴板,即剪切/复制和粘贴文本.也可以删除文本(与剪贴板无关).

I have a windows forms application with controls like textbox, combobox, datagridview etc. These controls allow a user to use the clipboad, i.e. cut/copy and paste text. It is also possible to delete text (which is not related to the clipboard).

我的应用程序有一个菜单栏,其中包含一个包含剪切/复制/粘贴/删除项目的编辑项目,以及一个包含这些项目的工具栏.如何根据具有焦点的控件的状态正确启用/禁用这些项目?

My application has a menubar with an Edit item containing Cut/Copy/Paste/Delete items, and a toolbar with these items as well. How can I enable/disable these items properly depending in the state of the control having the focus?

我正在寻找一种通用的方式,即我寻找一个我做过一次的实现,并且可以在未来重复使用,而与我的应用程序将使用的控件无关.

I am looking for a generic way, i.e. I look for an implementation I do once, and can reuse for the future independent of the controls my application will use.

推荐答案

没有用于从 Windows 窗体控件获取剪切/复制/粘贴信息的通用接口或方法集.

There is no generic interface or set of methods for getting cut/copy/paste information from a windows forms control.

我建议您最好的方法是为每种类型的控件创建一个包装类.然后,当您想要更新菜单状态时,您将获得具有焦点的当前控件并为其创建适当的包装器.然后您向该包装器询问您需要的状态信息.这样您只需为您使用的每种类型的控件创建一个包装器实现.开始有点痛苦,但其他时候您只需要添加您遇到的新控件.

I suggest your best approach would be to create a wrapper class for each type of control. Then when you want to update the menu state you get the current control with focus and create the appropriate wrapper for it. Then you ask that wrapper for the state information you need. That way you only need to create a wrapper implementation for each type of control you use. Bit of a pain to start with but other time you only need to add the new controls you come across.

剪贴板信息要容易得多,因为您可以询问剪贴板单例是否有数据以及它是什么类型.然后你仍然需要询问目标控件是否可以接受这种类型的信息,所以仍然需要做额外的工作.

Clipboard information is much easier as you can ask the Clipboard singleton if it has data inside and what type it is. Then again you still need to ask the target control if it can accept that type of information so there is still extra work needs doing.

这篇关于如何以通用方式启用/禁用剪切/复制/粘贴菜单和工具栏项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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