How to make redo and undo button to my app in vb.net2055? RRS feed

  • Question

  • How to make redo and undo button to my app in vb.net2055?

    Hi folks,

    I'm using the following code to crop images and it works properly.
    But now I want to know how to make "Redo" and "Undo" buttons 

    Private SelectSiz As Size
        Private SelectPos As System.Drawing.Point
        Private Selecting As Boolean = False
        Private Selected As Boolean = False
        Private source As Image
        Dim XOffs As Integer
        Dim YOffs As Integer
     If Selected Then
                    Dim Cropped As Image = New Bitmap(Math.Abs(SelectSiz.Width), Math.Abs(SelectSiz.Height))
                    Dim g As Graphics = Graphics.FromImage(Cropped)
                    Dim SrcRect As Rectangle = GetSelection(SelectPos, SelectSiz)
                    SrcRect.X += XOffs
                    SrcRect.Y += YOffs
                    g.DrawImage(source, New Rectangle(0, 0, Cropped.Width, Cropped.Height), SrcRect,                 GraphicsUnit.Pixel)
                    source = Cropped
                    PictureBox1.Image = Cropped
                End If

    Thank advance....
    Sunday, July 23, 2017 3:50 AM


All replies

  • Hi,

    for Images or other large dataobjects, it may be best to use a filesystem-cache for undo/redo operations. You could create a class Cache with Count, Reset, Undo, Redo properties and methods. Use eg. a folder in LocalApplicationData\yourappname.

    Generally you could consider using the Memento pattern, but also maybe the Command pattern:

    Important: Save the images as png images or other lossless encoding ways that fit your needs.



    Sunday, July 23, 2017 4:37 AM
  • How to make redo and undo button to my app in vb.net2055?

    You need to decide how that is going to work.

    If by 'undo' you mean to restore to how you started, then you still have the original image as 'source', so you can just set that as the image for the picturebox and you are back to the start.   If by 'redo' you mean to repeat what you just undid, then you need to keep a copy of SelectPos and SelectSiz, and execute the code in the button click event to repeat the crop operation.

    If you mean that you want to step back through multiple operations then you need to store information about each one.  For instance, it is possible for the user to select multiple different rectangles before doing the crop.  Do you mean that the system should remember those different selections to enable the user to recall them before doing the crop? 


    Sunday, July 23, 2017 6:19 AM
  • That depends if you want only one or more times undo possible. 

    However, in generally undo and redo is based on stacks where the object is added in advance of the action.


    • Marked as answer by Max45-1 Friday, July 28, 2017 12:43 AM
    Sunday, July 23, 2017 9:38 AM