Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C#

Undo/Redo Method for Databound Objects

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
4 Oct 2012CPOL7 min read 24.3K   322   13  
Method for implementing Undo/Redo functionality using Databound objects.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RestaurantForm
{
    public delegate void HealthScoreChangedEventHandler(object sender, HealthScoreChangedEventArgs e);
    public class HealthScoreChangedEventArgs : EventArgs
    {
        private HealthScore _oldvalue;
        private String _description;
        public HealthScoreChangedEventArgs(HealthScore oldvalue, String description) { _oldvalue = oldvalue.DeepClone(); _description = description; }
        public HealthScore OldValue { get { return _oldvalue; } }
        public String Description { get { return _description; } set { _description = value; } }
    }
    [Serializable]
    public class HealthScore
    {
        private int _value;
        public event HealthScoreChangedEventHandler Changed;
        public HealthScore() { _value = 0; }
        public HealthScore(int value) { _value = value; }
        public int Value
        {
            get { return _value; }
            set { OnChanged(new HealthScoreChangedEventArgs(this, "Value: " + this.Value + " to " + value.ToString())); _value = value; }
        }
        public int ValueSilent { set { _value = value; } }
        public HealthScore DeepClone()
        {
            HealthScore clone = new HealthScore();
            clone.ValueSilent = this.Value;
            return clone;
        }
        private void OnChanged(HealthScoreChangedEventArgs e) { if (Changed != null) Changed(this, e); }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions