Skip to main content Skip to footer

Implement Undo/Redo operations in a C1FlexGrid cell

Background:

How to implement Undo/Redo operations in a C1FlexGrid cell

Steps to Complete:

Currently, FlexGrid allows using CTRL+Z in order to undo last entered value in FlexGrid cell similar to what is done with MS TextBox.

To achieve typical Undo/Redo functionality using CTRL+Z and CTRL+Y key sequence in FlexGrid cell, use the code snippet given below:

public string[] RTBRedoUndo = = new string[10000];
 public int StackCount = 0;
 public int OldLength = 0;
 public int ChangeToSave = 5;
 public bool IsRedoUndo = false;
 public bool temp = true;
 RTBRedoUndo[0] = "";

//Define the cell editor as TextBox
private void C1FlexGrid1_StartEdit(object sender, C1.Win.C1FlexGrid.RowColEventArgs e){
c1FlexGrid1.Editor = new TextBox();}

//Set the Variables to their default values on cell change 
private void C1FlexGrid1_RowColChange(object sender, EventArgs e) {
        Array.Clear(RTBRedoUndo,0,RTBRedoUndo.Length);
        StackCount = 0;
        OldLength = 0;
        ChangeToSave = 5;
        IsRedoUndo = false;
        RTBRedoUndo = new string[10000];
        RTBRedoUndo[0] = "";
        temp = true;}

//Handle TextBox events to Undo/Redo in SetupEditor event of FlexGrid
private void C1FlexGrid1_SetupEditor(object sender, C1.Win.C1FlexGrid.RowColEventArgs e) {
            TextBox textBox1 = c1FlexGrid1.Editor as TextBox;
            textBox1.TextChanged += TextBox1_TextChanged;
            textBox1.KeyDown += TextBox1_KeyDown;        }

//Handle Undo/Redo operation on basis of Key Sequence entered
private void TextBox1_KeyDown(object sender, KeyEventArgs e){
         TextBox tb = sender as TextBox;
          if (e.Control && e.KeyCode == Keys.Z){
                IsRedoUndo = true;
                if (tb.TextLength > 0){
                    if(tb.Text.Substring(tb.Text.Length - 1, 1) !=" " && temp)  {
                        temp = false;
                        StackCount = StackCount + 1;
                        RTBRedoUndo[StackCount] = tb.Text;
                        OldLength = tb.Text.Length; }
                    if (StackCount > -1 && RTBRedoUndo[StackCount - 1] != null){
                        StackCount = StackCount - 1;
                        tb.Text = RTBRedoUndo[StackCount];}
                    IsRedoUndo = false;
                    tb.SelectionStart = tb.Text.Length;
                    tb.SelectionLength = 0;
                    e.Handled = false;  } }
            else if (e.Control && e.KeyCode == Keys.Y)  {
                IsRedoUndo = true;
                if (StackCount > -1 && RTBRedoUndo[StackCount +1] != null){
                    StackCount = StackCount + 1;
                    tb.Text = RTBRedoUndo[StackCount];  }
                IsRedoUndo = false;
                tb.SelectionStart = tb.Text.Length;
                tb.SelectionLength = 0;
                e.Handled = false;
            }  }
//Add strings in a list to be shown on Pressing Undo/Redo key Sequence 
        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (IsRedoUndo == false && tb.Text.Substring(tb.Text.Length-1, 1) == " ")
            {                
                StackCount = StackCount + 1;
                RTBRedoUndo[StackCount] = tb.Text;
        OldLength = tb.Text.Length;
            }  }  

Prabhat Sharma