How to add undo history

I want to add undo history in my desktop app and revert changes.

Can you tell us more about you app? How is the data stored? In a DB, in files?. What kind of data does it manage? How do you update data?

If we know you app better, we can make suggestions.

There is no database. it is a desktop app consist of three text boxes.

@Vansaj_Jain This is now your third forum entry for the same question in two days. I’m not an MVP, but be careful you don’t upset the moderators.

1 Like

One option to look at is The Big Undo by piDog.

1 Like

You’ll need to keep a list of prior entries, either in a database or maybe a string array.

Can you tell us if you have experience with other programming languages, or are you new to programming? This will help us provide appropriate help for you.

You’re at a good place to build the undo history, as your app appears to be simple so far. I have an application that keeps extensive history and allows undo and redo of almost anything. This was a design goal from the beginning, and the modules needed to make this work were written and tested before any of the app’s functional code or screens were created.

I would do it that way:

  • You have an array of dictionaries, let’s call it history
  • initial you fill a dictionary with the values of all controls on your window and its properties, which could be changed by the user (checkbox value, textbox text and so on).
  • add this dictionary to the history
  • When ever a controls value is changed, create a new dictionary with all properties of all controls and append it to history
  • So you collect the full change history of the user
  • when the user then wants undo a thing, simply step one item back in the historyarray and restore the values to all controls.
  • when the user steps back multiple times, always go back one index in the array.
  • when the user redo a step, go one step further in the history
  • when then the user changes a thing again, remove all items from history which come after the current loaded index, and append a new history-item.

You can make this quite easy very automated for one window if you always iterate through all controls of the window. But as soon you’ll have a very sophisticated user interface, you’ll have to deal with a more complex model.