How to enable / disable the Cut / Copy / Paste menu and toolbar items in general?

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

In my application, there is a menu bar with the "Edit" element containing the "Cut / Copy / Paste / Delete" elements, as well as a toolbar with these elements. How can I properly enable / disable these elements depending on the state of the control with focus?

I am looking for a general way, i.e. I'm looking for an implementation that I do once, and can reuse for the future regardless of the controls that my application will use.

+7
winforms
source share
2 answers

There is no common interface or set of methods for obtaining information about cutting / copying / pasting from a window control.

I suggest that your best approach is to create a wrapper class for each type of control. Then, when you want to update the status of the menu, you get the current control with focus and create an appropriate shell for it. You then request this wrapper for the status information that you need. Thus, you only need to create a shell implementation for each type of control you are using. A bit of a pain to start with, but at other times you only need to add the new controls that you encounter.

The information in the clipboard is much simpler, since you can request a single-line clipboard if it contains data inside and what type. Then again you need to ask the target management if it can accept this type of information so that additional work still needs to be done.

+3
source share

Create an array for each on / off group. Add controls to the array (of course, it should be of the type Object or Any, etc. It depends on the programming language that you use). Then, to enable, disable only the loop through the array and call the on / off method / function for each control. Again, depending on the language you may need to cancel.

0
source share

All Articles