Computing.Net > Forums > Programming > Undo/Redo in C#

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Undo/Redo in C#

Reply to Message Icon

Name: web_scripter
Date: November 9, 2004 at 16:00:12 Pacific
OS: Win XP
CPU/Ram: amd athlon/ 320mb ram
Comment:

I have a pictureBox and i have a custom class called rectangle when i click a button i draw the rectangle on the screen and i can move it around,change color, ect.
How do i create a undo/redo feature so that when i move the rectangle it adds the action to a history and when i click a undo button it moves it back to the previous state. And when i click the redo button it moves the rectangle to the next state if i clicked undo.




Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: November 10, 2004 at 02:21:03 Pacific
Reply:

I suggest using a collection class like an array list. create a class to hold the information you wish to store (position and color) and then everytime this changes, add another item to the list:

example:

// This class will implement an UNDO
public class UndoCLASS
{
// use an array list instead of a stack so that you can list all elements.
private System.Collections.ArrayList UndoStack = new ArrayList();
public UndoCLASS()
{
}
----------------------

----------------------
// FUNCTION: PUSH
// PURPOSE: To add items to the undo list
----------------------
public void Push(System.Drawing.Rectangle rectangle,System.Drawing.Color color)
{
StorageCLASS NewItem = new StorageCLASS();
NewItem.rectangle = rectangle;
NewItem.color = color;
UndoStack.Add(NewItem);
}
----------------------

----------------------
// FUNCTION: Pop
// PURPOSE: To get last item added to the list
----------------------
public void Pop(ref System.Drawing.Rectangle rectangle,System.Drawing.Color color)
{
StorageCLASS OldItem = ((StorageCLASS)UndoStack[UndoStack.Count-1]);
rectangle = OldItem.rectangle;
color = OldItem.color;
UndoStack.RemoveAt(UndoStack.Count-1);
}
----------------------

----------------------
// FUNCTION: ListStack
// PURPOSE: To list stack into a combo box
----------------------
public void ListStack(ref System.Windows.Forms.ComboBox combo)
{
StorageCLASS ListItem = new StorageCLASS();
combo.Items.Clear();
for(int t=UndoStack.Count-1;t>-1;t--)
{
ListItem = ((StorageCLASS)UndoStack[t]);
combo.Items.Add("[" + ListItem.rectangle.Left.ToString() + "," + ListItem.rectangle.Top.ToString() + "," + ListItem.rectangle.Width.ToString() + "," + ListItem.rectangle.Height.ToString() + "];[" + ListItem.color.ToString() + "]");
}
}
----------------------
}

// simple storage class for storing the rectangle and color
public class StorageCLASS
{
private System.Drawing.Rectangle lRect = new Rectangle();
private System.Drawing.Color lColor = new Color();
public StorageCLASS()
{
}
public System.Drawing.Rectangle rectangle
{
set
{
lRect = value;
}
get
{
return lRect;
}
}
public System.Drawing.Color color
{
set
{
lColor = value;
}
get
{
return lColor;
}
}
}


Enjoy.
Chi

"They mostly come at night...mostly"


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Undo/Redo in C#

Pointers in C/C++ www.computing.net/answers/programming/pointers-in-cc/876.html

what does this func do in C www.computing.net/answers/programming/what-does-this-func-do-in-c/9528.html

Null Character in C? (NOT '\0') www.computing.net/answers/programming/null-character-in-c-not-0/3201.html