CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: UnDo operation

  1. #1
    Join Date
    Sep 2005
    Posts
    117

    Cool UnDo operation

    What should be the way to implement UnDo operation in the GUI. I heard that with "command pattern" we should do it.

    Ideas are welcome!

  2. #2
    Join Date
    Jan 2007
    Posts
    491

    Re: UnDo operation

    Undo of what operations?
    Anyways, one way to do that would be to keep a list of all recent operations (you're gonna have to implement a Operation class with the information of the proper operation), and once the undo button is clicked, cancel the most recent one (Operation needs to have a Cancel() method) and delete it from the list of operations.

  3. #3
    Join Date
    Sep 2005
    Posts
    117

    Re: UnDo operation

    I have a MDI, and I want activities in the child windows to be tracked upon (like editing textbox, combo box etc)

  4. #4
    Join Date
    Jan 2007
    Posts
    491

    Re: UnDo operation

    okay, I don't think there's a built-in way to do that.

    So as I said, you can create an Operation class that will record such activities.
    here's a quick example for how to create one to combo box selection change:
    Code:
    abstract class Operation //you could also use an interface here
    {
        public abstract void Cancel() {}
    }
    ...
    class ComboBoxSelectedIndexOperation : Operation
    {
        ComboBox comboBox;
        int oldIndex;
    
        public ComboBoxSelectedIndexOperation(ComboBox comboBox, int oldIndex)
        {
              this.comboBox = comboBox;
              this.oldIndex = oldIndex;
        }
    
        private override void Cancel()
        {
               comboBox.SelectedIndex = oldIndex;
               comboBox.Update();
        }
    }
    '"
    Now, every time the selection of a combobox is changed, you create a new instance of the ComboBoxSelectedIndexOperation class, and add it to "List<Operation> operations".

    Once the user clicks the "Undo" button, you check whether or not this list is empty.
    If it is, you do nothing (since there aren't any activities to undo).
    If it isn't, you cancel the last operation in that list, and delete it from the list.
    i.e. something like that:
    Code:
    //inside your MDI form:
    private void Undo()
    {
         if(this.operations.Count == 0)
             return;
         operations[opertaion.Count-1].Cancel();
         operations.RemoveAt(opertaion.Count-1);
    }
    }

    of course you're gonna have to implement a class for every undo-able operation that you want to support. might be a little frustrating, but as you can see it's pretty easy.
    Last edited by Talikag; September 6th, 2010 at 10:40 PM.

  5. #5
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: UnDo operation

    Use an 'Undo' Stack. Each time the User makes a change to the document push an entry on to the stack. When the User chooses to Undo, pop an entry off of the stack (this entry could also be pushed onto a 'Redo' stack). A Redo option would do the opposite.

    The entries could be the whole document. This is easy to implement, but could use up a lot of resources depending on the size of the document. An option here would be to limit the size of the 'Undo' and 'Redo' stacks. Otherwise push an 'Operation' onto the stack, similar to what Talikag has shown. This is harder to implement as specific details of the actual change need to be recorded, however there should be less overhead.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  6. #6
    Join Date
    Sep 2010
    Posts
    2

    Smile Re: UnDo operation

    Hi

    I searched for ages how to implement Undo etc. But go to www.undomadeeasy.com. It cracked it for me.

    Regards Carl

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: UnDo operation

    From their description and the size of their assembly, this could be rewritten in approximately 30 minutes. Call it 2 hours including tests and docs. I'll charge you $30 for a functionally equivalent library Seriously, it's unbelievably trivial to write an undo/redo engine like they've done. The hard part is integrating it with your application as every 'do' and 'undo' has to go through the engine. If you miss a place, then the undo/redo buttons won't do what you expect.

    EDIT: also advertising your own product is frowned upon.

    EDIT2: Actually, my hats off to you. Even at two sales you're making profit. Not bad!
    Last edited by Mutant_Fruit; October 12th, 2010 at 12:39 PM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  8. #8
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: UnDo operation

    I'll do it for $29.99. (AUD)


    Mutant.... Those 2xEDIT's you did. You could have saved all that time, if only you had been using the UndoEngine!!
    Last edited by rliq; October 13th, 2010 at 03:50 AM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured