如何以通用方式启用/禁用剪切/复制/粘贴菜单和工具栏项?

时间:2022-03-25 15:51:10

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).

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

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.

我正在寻找一种通用的方式,即我寻找一次我执行的实现,并且可以在将来重用,而不依赖于我的应用程序将使用的控件。

2 个解决方案

#1


3  

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

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

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.

剪贴板信息更容易,因为你可以询问剪贴板单例是否有内部数据和它是什么类型。然后,您仍然需要询问目标控件是否可以接受该类型的信息,因此仍有额外的工作需要。

#2


0  

Create an array for each enable/disable group. Add the controls to the array (of course it has to be of the correct type such as Object or Any, etc. depends on the programming language you are using). Then to enable, disable just loop through the array and invoke the enable/disable method or function for each control. Again, depending on the language you may need to cast back.

为每个启用/禁用组创建一个数组。将控件添加到数组中(当然它必须是正确的类型,如Object或Any等,取决于您使用的编程语言)。然后启用,禁用循环数组并为每个控件调用启用/禁用方法或函数。同样,根据您可能需要回退的语言。

#1


3  

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

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

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.

剪贴板信息更容易,因为你可以询问剪贴板单例是否有内部数据和它是什么类型。然后,您仍然需要询问目标控件是否可以接受该类型的信息,因此仍有额外的工作需要。

#2


0  

Create an array for each enable/disable group. Add the controls to the array (of course it has to be of the correct type such as Object or Any, etc. depends on the programming language you are using). Then to enable, disable just loop through the array and invoke the enable/disable method or function for each control. Again, depending on the language you may need to cast back.

为每个启用/禁用组创建一个数组。将控件添加到数组中(当然它必须是正确的类型,如Object或Any等,取决于您使用的编程语言)。然后启用,禁用循环数组并为每个控件调用启用/禁用方法或函数。同样,根据您可能需要回退的语言。