Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

C# undo/redo history (Ctrl-Z, Ctrl-Y)

Newbie Spellweaver
Joined
Jan 1, 2009
Messages
98
Reaction score
21
This script is used to replicate the actions of deletion, addition, moves, ect, and make it possible to fully undo and redo.

Coded By: brclancy111
For use with: Controls

To Use:
1->Make history public class
2->Use Add(HISTORYOBJECT) to replicate a Undo Point
3->Use Undo() to go to the Last undo
4->Use Redo() to restore it.

on Add(HISTORYOBJECT), you have to know what type of undo it is, and the parameters of the object.

You can also modify it to edit a List<>, so you can easily edit draw objects.


Code:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Windows.Forms;

/*Written by brclancy111
 * brclancy111@hotmail.com
 * 
 * USE:
    History H = new History();
    HistoryObject HO = new HO();
    HO.ObjectToBeActed = PANEL1;
    HO.Type = HistTypes.delete;
    H.Add(HO);

 * */

namespace IdeaBuilder
{
    interface Command
    {
        void Undo();
        void Redo();
    }
    class HistoryObject : Command
    {
        public HistTypes Type = HistTypes.none;
        public Control ObjectToBeActed;
        public ArrayList Perameters = new ArrayList(); //used for data to be stored. This can include other controls, ect.
        /*
         * OParent
         * NParent
         * 
         * OLeft
         * OTop
         * NLeft
         * NTop
         * 
         * OText
         * NText
         * */

        public void Undo()
        {
            try
            {
                if (Type != HistTypes.none && ObjectToBeActed != null)
                {
                    if (Type == HistTypes.add)
                    {
                        //Add To the old parent...
                        //PerameterData: Old, New parents.
                        //Control OParent = (Control)Perameters[0];
                        Control NParent = (Control)Perameters[0];
                        //OParent.Controls.Remove(ObjectToBeActed);
                        NParent.Controls.Add(ObjectToBeActed);
                    }
                    if (Type == HistTypes.delete)
                    {
                        //Add To the old parent...
                        //PerameterData: Old parent.
                        Control OParent = (Control)Perameters[0];
                        //Control NParent = (Control)Perameters[1];
                        OParent.Controls.Remove(ObjectToBeActed);
                        //NParent.Controls.Add(ObjectToBeActed);
                    }
                    if (Type == HistTypes.cut)
                    {
                        Control OParent = (Control)Perameters[0];
                        OParent.Controls.Add(ObjectToBeActed);
                    }
                    if (Type == HistTypes.move)
                    {
                        ObjectToBeActed.Left = (int)Perameters[0];
                        ObjectToBeActed.Top = (int)Perameters[1];
                    }
                    if (Type == HistTypes.moveparent)
                    {
                        ObjectToBeActed.Left = (int)Perameters[0];
                        ObjectToBeActed.Top = (int)Perameters[1];
                        Control OParent = (Control)Perameters[4];
                        OParent.Controls.Remove(ObjectToBeActed);
                        Control NParent = (Control)Perameters[5];
                        NParent.Controls.Add(ObjectToBeActed);
                    }
                    if (Type == HistTypes.size)
                    {
                        ObjectToBeActed.Width = (int)Perameters[0]; //2 for older coords...
                        ObjectToBeActed.Height = (int)Perameters[1];
                    }
                    if (Type == HistTypes.text)
                    {
                        ObjectToBeActed.Text = (string)Perameters[0]; //1 for older string.
                    }
                }
            }
            catch { }
        }

        public void Redo()
        {
            try
            {
                if (Type != HistTypes.none && ObjectToBeActed != null)
                {
                    if (Type == HistTypes.add)
                    {
                        //Add To the old parent...
                        //PerameterData: Old, New parents.
                        //Control OParent = (Control)Perameters[0];
                        Control NParent = (Control)Perameters[1];
                        //OParent.Controls.Remove(ObjectToBeActed);
                        NParent.Controls.Add(ObjectToBeActed);
                    }
                    if (Type == HistTypes.delete)
                    {
                        //Add To the old parent...
                        //PerameterData: Old parent.
                        Control OParent = (Control)Perameters[1];
                        //Control NParent = (Control)Perameters[1];
                        OParent.Controls.Remove(ObjectToBeActed);
                        //NParent.Controls.Add(ObjectToBeActed);
                    }
                    if (Type == HistTypes.cut)
                    {
                        Control OParent = (Control)Perameters[1];
                        OParent.Controls.Add(ObjectToBeActed);
                    }
                    if (Type == HistTypes.move)
                    {
                        ObjectToBeActed.Left = (int)Perameters[2];
                        ObjectToBeActed.Top = (int)Perameters[3];
                    }
                    if (Type == HistTypes.moveparent)
                    {
                        ObjectToBeActed.Left = (int)Perameters[1];
                        ObjectToBeActed.Top = (int)Perameters[2];
                        Control OParent = (Control)Perameters[5];
                        OParent.Controls.Remove(ObjectToBeActed);
                        Control NParent = (Control)Perameters[4];
                        NParent.Controls.Add(ObjectToBeActed);
                    }
                    if (Type == HistTypes.size)
                    {
                        ObjectToBeActed.Width = (int)Perameters[2]; //2 for older coords...
                        ObjectToBeActed.Height = (int)Perameters[3];
                    }
                    if (Type == HistTypes.text)
                    {
                        ObjectToBeActed.Text = (string)Perameters[1]; //1 for older string.
                    }
                }
            }
            catch { }
        }
    }
    enum HistTypes { none, delete, add, cut, move, size, text, moveparent };
    class History
    {
        public static ArrayList Undos = new ArrayList(), Redos = new ArrayList();
        public void Undo()
        {
            Command C = (Command)Undos[Undos.Count - 1];
            C.Undo();
            Undos.RemoveAt(Undos.Count - 1);
            Redos.Add(C);
        }
        public void Redo()
        {
            Command C = (Command)Redos[Redos.Count - 1];
            C.Redo();
            Redos.RemoveAt(Redos.Count - 1);
            Undos.Add(C);
        }

        public void Add(HistoryObject command)
        {
            Undos.Add(command);
        }
    }
}
 
Back
Top