Share:

Create UndoRedoManager V1_0_0 For WPF C#

 In post, I will talk about undo and redo manager (UndoRedoManager.cs) for C# (WPF and WindowsForm both). It's works properly in WPF but, I can't take responsibility for WindowsForm. Because this functionality tested in WPF only. So, This is version V1_0_0 and the below is code of UndoRedoManager.cs.

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Markup;
using System.Xml;

namespace Saatody
{
    public class UndoRedoManager
    {
        public MainWindow Window { set; get; }

        private int Pointer = 0;
        private List<string> States = new List<string>();

        public void RecordState(object State)
        {
            try
            {
                States.RemoveRange(Pointer + 1, States.Count - (Pointer + 1));
            }
            catch(Exception Ex) { }

            States.Add(XamlWriter.Save(State));
            Pointer = States.Count - 1;

            Window.Console.Text = "Record State (" + Pointer + "): " + XamlWriter.Save(State) + "\n" + Window.Console.Text;
        }
        public object UndoState()
        {
            if (Pointer > 0)
            {
                Pointer -= 1;
            }

            Window.Console.Text = "Undo State (" + Pointer + "): " + States[Pointer] + "\n" + Window.Console.Text;

            StringReader StringReader = new StringReader(States[Pointer]);
            XmlReader XmlReader = XmlReader.Create(StringReader);
            object State = (object)XamlReader.Load(XmlReader);
            StringReader.Close();
            XmlReader.Close();

            return State;
        }
        public object RedoState()
        {
            if (Pointer < (States.Count - 1))
            {
                Pointer += 1;
            }

            Window.Console.Text = "Redo State (" + Pointer + "): " + States[Pointer] + "\n" + Window.Console.Text;

            StringReader StringReader = new StringReader(States[Pointer]);
            XmlReader XmlReader = XmlReader.Create(StringReader);
            object State = (object)XamlReader.Load(XmlReader);
            StringReader.Close();
            XmlReader.Close();

            return State;
        }
    }
}
There is three functions available and responsible for entire process.
  • RecordState()
  • UndoState()
  • RedoState()

RecordState() - Record An Object As State

In RecordState(), User need to pass an object to record in list. There doesn't matter, what kind of object you passed. List never auto-store next state of object in List<State> list until you manually call this function again.

Syntax of RecordState()

public void RecordState(object State)

UndoState() - Undo State From List

In UndoState(), User will get an object according to states position. Now user need to manually casting and update that object. The function UndoState(), You can use in the Keyup event where Ctrl + Z shortcut you applied or you can use on any button or menu click event.

Syntax of UndoState()

public object UndoState()

RedoState() - Redo State From List

In RedoState(). User will get an object according to states position. After getting an object, User need to manually casting that object. This function user can use in Keyup event where Ctrl + Y shortcut you applied.

Syntax of RedoState()

public object RedoState()

In Second post, I will give you an example that why you can understand how you can use this API in your application.

Checkout my second post on UndoRedoManager.cs example: https://me-saatody.blogspot.com/2020/09/example-of-undoredomanager-v100-wpf-c.html

Comments

Popular posts from this blog

Get Color From Pixel C# WPF | Saatody | Amit Padhiyar

Basic Audio Operations With MP3 And Wave Files Using NAudio C#

Create Drag And Drop Operation Between DevExpress GridControl And Custom WPF UI | Saatody | Amit Padhiyar