Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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"

![]() |
![]() |
![]() |

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