Multiple Undo/Redo

Rioven

Active Member
Licensed User
What are nice ways to implement multiple undo and redo on applications like
text editor or table entries?
 

Erel

Administrator
Staff member
Licensed User
The following code implements multiple Undo in a table.
This code will not work if the value of a cell contains a comma.
You can change it to work with some other character like tilde (~) instead.
alStack is an ArrayList.
B4X:
Sub Globals
    'Declare the global variables here.
    Dim Type(Column,Row,Value) UndoItem
End Sub

Sub App_Start
    Form1.Show
    FillTable
End Sub

Sub FillTable
    Table1.AddCol(cString,"Col 1",50)
    Table1.AddCol(cString,"Col 2",50)
    Table1.AddCol(cString,"Col 3",50)
    For i = 1 To 10
        Table1.AddRow(i,i,i)
    Next
End Sub

Sub AddItem (column,row,oldValue)
    alStack.Insert(0,column & "," & row & "," & oldValue)
End Sub

Sub UndoLast
    If alStack.Count = 0 Then Return 'No items
    UndoItem() = StrSplit(alStack.Item(0),",")
    alStack.RemoveAt(0)
    Table1.Cell(UndoItem.Column,UndoItem.Row) = UndoItem.Value
End Sub

Sub btnChange_Click
    col = Table1.SelectedCol
    row = Table1.SelectedRow
    AddItem(col,row,Table1.Cell(col,row)) 'Save the previous value
    Table1.Cell(col,row) = txtChange.Text
End Sub
Sub btnUndo_Click
    UndoLast
End Sub
 

Rioven

Active Member
Licensed User
Hi Erel, Thanks for this useful sample code for table.:)
 
Top