Making an Undo button for use with drawing tools

538
1
Jump to solution
08-08-2013 09:07 AM
BobNichols
New Contributor III
I have an application that allows my users to create simple maps during planning meetings.  I am using parts of the "Add interactively"  sample found here.  In addition to the clear graphics button, like in the example, I want to code up something that would allow an "undo" action in case the user makes a mistake.  Even if it only steps back one drawing action, that would be a whole lot better than starting over with the clear button.

I have been looking at the UndoLastVertex method within the Draw class, but am having issues with getting it to work.  There is a very good chance that I am using it wrong and was hoping someone could point me in the right direction or provide an example of what I am trying to accomplish.

What I am trying now is simply running the line of code below when the "Undo" button is clicked.

MyDrawObject.UndoLastVertex();

Again, I have to imagine that I am missing something.  For example, does the graphics layer need to be in some sort of edit session while the user is drawing? Or is it as simple as the line of code above and I have something else screwy going on?

Thanks in advance!
0 Kudos
1 Solution

Accepted Solutions
BobNichols
New Contributor III
I got it figured out and thought I should post to help the next person out.  Found parts of this code buried in these forums and modified it to work like I wanted it to.  Right now I have the code below wired up the keyboards delete key, but the code can easily be modified to work with a button in your app. When the delete key is pressed the last object drawn to the graphics layer is deleted.  You can continue to hit delete until the last object on your graphics layer is removed. 

void MyMap_KeyDown(object sender, KeyEventArgs e)         {             if (e.Key == Key.Delete)             {                 var l = MyMap.Layers["Drawing Layer"] as GraphicsLayer;                 if (l != null)                 {                     var graphic = l.Graphics.LastOrDefault(g => g.Geometry != null);                     if (graphic != null)                         l.Graphics.Remove(graphic); //check if this returns true/false.                 }              }         }

View solution in original post

0 Kudos
1 Reply
BobNichols
New Contributor III
I got it figured out and thought I should post to help the next person out.  Found parts of this code buried in these forums and modified it to work like I wanted it to.  Right now I have the code below wired up the keyboards delete key, but the code can easily be modified to work with a button in your app. When the delete key is pressed the last object drawn to the graphics layer is deleted.  You can continue to hit delete until the last object on your graphics layer is removed. 

void MyMap_KeyDown(object sender, KeyEventArgs e)         {             if (e.Key == Key.Delete)             {                 var l = MyMap.Layers["Drawing Layer"] as GraphicsLayer;                 if (l != null)                 {                     var graphic = l.Graphics.LastOrDefault(g => g.Geometry != null);                     if (graphic != null)                         l.Graphics.Remove(graphic); //check if this returns true/false.                 }              }         }
0 Kudos