Haim Evgi
Haim Evgi

Reputation: 125446

c# undo in windows form application

I want to know what is the easy way to develop an undo in c#

i have a windows form application, in the window i have a form with a lot of text box , select box , checkbox ...

after the user fill in and click on calculate button, then the program calculate according to all the inputs and return a value to the screen.

i search an easy way to save the states after every calculte , and give the user the abbility to return one, or more steps back .

thanks

Upvotes: 0

Views: 7234

Answers (5)

Rizwan
Rizwan

Reputation: 1

Its pretty late, but just got a query from friend yesterday.

I developed this simple undo redo functionality in richtextbox. Yes there are many better ways but just as a quick solution it can be used.

using System;
using System.Windows.Forms;

namespace Undo_Redo
{
    public partial class Form1 : Form
    {
        public string[] RTBRedoUndo;
        public int StackCount = -1;
        public bool IsRedoUndo = false;
        public string[] RTBRedo;
        public int RedoStackCount = -1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RTBRedoUndo = new string[10000];
            RTBRedo = new string[10000];

        }
        private void btn_undo_Click(object sender, EventArgs e)
        {

            if (StackCount > -1 && RTBRedoUndo[StackCount] != null)
            {
                IsRedoUndo = true;
                RedoStackCount += 1;
                RTBRedo[RedoStackCount] = RTBRedoUndo[StackCount];
                richTextBox1.Text = richTextBox1.Text.Substring(0, richTextBox1.Text.Length - 1);
                RTBRedoUndo[StackCount] = null;
                StackCount -= 1;

            }
        }

        private void btn_redo_Click(object sender, EventArgs e)
        {
            if (RedoStackCount > -1 && RTBRedo[RedoStackCount] != null)
            {
                IsRedoUndo = true;
                StackCount += 1;
                RTBRedoUndo[StackCount] = RTBRedo[RedoStackCount];
                richTextBox1.Text = richTextBox1.Text + RTBRedo[RedoStackCount];
                RTBRedo[RedoStackCount] = null;
                RedoStackCount -= 1;
            }
        }

        private void btn_redo_MouseUp(object sender, MouseEventArgs e)
        {
            IsRedoUndo = false;
        }

        private void btn_undo_MouseUp(object sender, MouseEventArgs e)
        {
            IsRedoUndo = false;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (IsRedoUndo == false)
            {
                StackCount += 1;
                RTBRedoUndo[StackCount] = richTextBox1.Text.Substring(richTextBox1.Text.Length - 1, 1);
            }
        }
    }
}

Upvotes: 0

Simon Mourier
Simon Mourier

Reputation: 138776

I don't think there is any standard built-in, but you can find some frameworks to do it yourself: The Undo Framework with a sample for Forms: Samples for the Undo Framework

There is also the Monitored Undo Framework.

Upvotes: 5

Ed Swangren
Ed Swangren

Reputation: 124622

A simple implementation would be a stack. As the user performs operations, push the current value onto the stack. As they 'undo', pop values off of the stack.

Upvotes: 2

Javed Akram
Javed Akram

Reputation: 15344

If you are using Textbox. You can Undo it by 1 step back by using `Undo() method n

txtBoxResult.Undo();

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67175

The easiest way is to simply save the input text somewhere.

You could get more fancy by encoding, somehow, only the text that has changed from the last time the input was saved. For example, you could store only new text that was added along with the position it was added. Or you could store only the position and length of text that was deleted. But that's more complex and the effect is the same.

While a text box does have a simple undo feature built in, if that doesn't work for you, you'll just have to save the text at each undo point. It's easy, simple, and necessary.

Upvotes: 0

Related Questions