class MyForm{ //stack object for holding the redo bitmap states private Stack<Bitmap> redoStates = new Stack<Bitmap>(); //stack object for holding the undo bitmap states private Stack<Bitmap> paintStates = new Stack<Bitmap>(); //ctor public MyForm(){ //this form will receive keyboard events before any other control does KeyPreview = true; //override on key down event to listen for undo, redo, paste and copy events KeyDown += OnKeyDown; } private void OnKeyDown(object? sender, KeyEventArgs e) { //check if the user is trying to undo if (e.Control && e.KeyCode == Keys.Z) { //the user is trying to undo the last paint action if (paintStates.Count > 0) { //add this bitmap to the redo stack before removing it redoStates.Push(paintStates.Peek()); //remove the last added bitmap paintStates.Pop(); drawingPanel.Image = paintStates.Peek(); } } //check if the user is trying to redo if(e.Control && e.KeyCode == Keys.Y) { //the user is trying to redo the paint events //get the object pushed out and display it if (redoStates.Count > 1) { //reverse the redo states drawingPanel.Image = redoStates.Peek(); } } } }
using System; using System.Collections.Generic; public class Program { public static void Main() { Stack<int> s = new Stack<int>(); for (int i = 0; i < 10; i++) { s.Push(i); } int[] arr = s.ToArray(); // Forward for (int i = arr.Length - 1; i >= 0; i--) { Console.WriteLine(arr[i]); } // Backward while (s.Count > 0) { Console.WriteLine(s.Pop()); } } }
class MyForm{ //class level declaration of the stack to hold the draw commands private Stack<DrawItem> drawCommands = new Stack<DrawItem>(); //event handler when the user clicks two points on the picture box to draw a line private void DrawingPanelOnMouseClick(object? sender, MouseEventArgs e) { //increase the indexer if the app is listening if (listening) { indexer += 1; } //get the point clicked var point = new Point(e.X, e.Y); pointsClicked.Add(point); //add the point to a list if (indexer == 2 && drawItem == "Line") { //push object and points to stack drawCommands.Push(new DrawItem("Line",pointsClicked)); //get the context and draw the line drawingPanel.CreateGraphics().DrawLine(drawingPen,pointsClicked[0],pointsClicked[1]); //reset the indexer indexer = 0; //clear the list of points clicked pointsClicked.Clear(); } //draw as many lines as possible and then try undoing with this method private void OnKeyDown(object? sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.Z) { //the user is trying to undo the last paint action //if the user has performed only one draw command clear the pane if (drawCommands.Count == 1) { SetPixel(drawingPanel); } if (drawCommands.Count > 1) { //push the command at the top of the stack to the redo command list before deleting it redo.Push(drawCommands.Peek()); //remove the command at the top of the stack drawCommands.Pop(); //loop through the remaining commands and redraw them on the panel foreach(var command in drawCommands){ //get the command text switch(command.Text){ case "Line": drawingPanel.CreateGraphics().DrawLine(drawingPen,command.Points[0],command.Points[1]); break; } } } } } } //implementation of my drawitem class public class DrawItem { private string _itemShape; private List<Point> shape = new List<Point>(); //require the two inside the constructor public DrawItem(string itemShape, List<Point> points) { _itemShape = itemShape; shape = points; } //define the getters and setters for the properties public string Item { get { return _itemShape; } } public List<Point> Points { get { return shape; } } }
drawCommands.Pop(); ^ | ... foreach(var command in drawCommand){ ^ |
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)