Custom copy / paste behavior using winforms C # allowing you to edit or copy text

I currently have a C # winforms application with Ctrl + C and Ctrl + V, linked as a keyboard shortcut in the Edit main menu. Inside the code, there is some user-defined copy / paste that responds to these menu items, for example, copying and pasting lines in view lists.

However, my problem is that you can edit the text inside the line, and when you do this, I want Ctrl + C and Ctrl + V not to run the edit menu command and should use the standard text copy / paste by default.

One of my attempts is to fire BeforeLabelEdit and AfterLabelEdit events and manually disable / re-enable menu items from the inside. Unfortunately, it seems that the key combination in the disabled menu item still raises the menu_Popup event, which is currently used to determine which menu items should be enabled / disabled. (For example, "Paste" is only active if there is text in the clipboard). Therefore, even if I turn off the menu item, the key combination will still trigger a pop-up event that will turn on the menu item again. (This is mistake?)

I cannot find any method to temporarily disable the menu shortcut without manually storing the old shortcut, setting the shortcut to zero, and then copying it back when I need to turn it back on (which seems dirty).

Of course, redefining the behavior of copy / paste or adding to it is the usual thing to do? Is there a better sample to use here?

+5
source share
1 answer

It looks like you need to use something lower than C # if you want to override the default copy / paste behavior (see C # clipboard event and Detecting and differentiating clipboard events (cut, copy and paste) ). However, perhaps you could put your logic in a “defender” that knows how to send a request for an action (for example, “Copy”), and then redirect it as appropriate.

Here is an example class:

namespace Your.App
{
    public class GuardedCommand
    {
        public bool CurrentlyEditing { get; set; }
        public GuardedCommand()
        {
            CurrentlyEditing = false;
        }
        public void DoCopy()
        {
            if(CurrentlyEditing)
                StandardCopyCommand();
            else
                ShortcutCopyCommand();
        }
        void ShortcutCopyCommand() { /*menu work here (or delegate to another class)*/ }
        void StandardCopyCommand() { /*"normal" work here (or delegate again)*/ }
    }
}

, guardedCommand.CurrentlyEditing BeforeLabelEdit AfterLabelEdit. , CTRL + C, guardedCommand.DoCopy(), .

, , State Pattern, . ( ), , DoCopy() DoPaste(), if/else switch. , CurrentlyEditing , ​​ , DoCopy().

, , , , - :

namespace Your.App
{
//correct implementation of the State Pattern
    interface IClipboard
    {
        void Copy();
        void Paste();
    }
    class MyCustomClipboard : IClipboard
    {
        public void Copy() { /*your special code*/ }
        public void Paste() { /*your code again*/ }
    }
    class DefaultClipboard : IClipboard
    {
        public void Copy() { /*default code*/ }
        public void Paste() { /*default code again*/ }
    }
    public class StateClass
    {
        IClipboard State { get; set; }
        public StateClass()
        {
            CurrentlyEditing = false;
        }
        bool _currentlyEditing;
        public bool CurrentlyEditing
        {
            get { return _currentlyEditing; }
            set
            {
                _currentlyEditing = value;
                if(_currentlyEditing)
                    State = new DefaultClipboard();
                else
                    State = new MyCustomClipboard();
            }
        }
        public void Copy()
        {
            State.Copy();
        }
        public void Paste()
        {
            State.Paste();
        }
    }
}

, , ( , , ).

0

All Articles