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; } } }