private void redoCellTextChangeToolStripMenuItem_Click(object sender, EventArgs e) { ICmd temp = mySpreadsheet.popRedo(); mySpreadsheet.AddUndo(temp.Exec()); undoCellTextChangeToolStripMenuItem.Enabled = true; }
//update the cell value in the dataGridView1 private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { var cellVal = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; string newVal = String.Empty; if (cellVal != null) { newVal += cellVal.ToString(); } Spreadsheet.SpreadsheetCell cell = ss.SS_Array[e.RowIndex, e.ColumnIndex]; List <IInvertibleCmd> cmds = new List <IInvertibleCmd>(); if (cell.newCellText == null || (cell.cellValue != newVal && cell.newCellText != newVal)) { cell.newCellText = newVal; cmds.Add(new restoreText(cell)); } if (cmds.Count > 0) { ss.AddUndo(new MultiCmd("Undo cell text", cmds)); undoToolStripMenuItem.Enabled = true; undoToolStripMenuItem.Text = "Redo cell text"; } }
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { int row = e.RowIndex; int col = e.ColumnIndex; string updateText; Cell sheetCell = mSheet.GetCell(row, col); try { // gather the updated text from the cell updateText = dataGridView1.Rows[row].Cells[col].Value.ToString(); } catch (NullReferenceException) { updateText = ""; } IUndoRedoCmd[] undos = new IUndoRedoCmd[1]; undos[0] = new RestoreTextCmd(sheetCell.Text, sheetCell.Name); // set the current selected cells text to the updated text sheetCell.Text = updateText; // since this function is related to cell text, we add a change in // cell text undo to the internal undo stack mSheet.AddUndo(new UndoRedoCollection(undos, "change in cell text")); // then set the value of the cell to the current cells internal value dataGridView1.Rows[row].Cells[col].Value = sheetCell.Value; UpdateToolStrip(); }
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { // if expression then show value var val = dataGridView1[e.ColumnIndex, e.RowIndex].Value; if (val != null) { Cell cell = Sheet.GetCell(e.RowIndex, e.ColumnIndex); Sheet.AddUndo(new UndoRedoCollection(UndoRedoType.Text, new UndoRedoText(cell, cell.Text))); cell.Text = val.ToString(); dataGridView1[e.ColumnIndex, e.RowIndex].Value = cell.Value; } updateUndoRedoMenuItems(); }
private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { Cell currentCell = this.test.GetCell(e.RowIndex, e.ColumnIndex); dataGridView[e.ColumnIndex, e.RowIndex].Value = currentCell.Text; Color newColor = Color.FromArgb((int)currentCell.BGColor); dataGridView[e.ColumnIndex, e.RowIndex].Style.BackColor = newColor; ICmd text = new RestoreText(currentCell, currentCell.Text); ICmd bgcolor = new RestoreBGColor(currentCell, currentCell.BGColor); ICmd multi = new MultiCmd(); MultiCmd temp = multi as MultiCmd; temp.Add(text); temp.Add(bgcolor); test.AddUndo(multi); }
private void dataGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e) { var cell = ss.GetCell(e.ColumnIndex, e.RowIndex); if (dataGrid.Rows[cell.RowIndex].Cells[cell.ColIndex].Value != null) { string value = dataGrid.Rows[cell.RowIndex].Cells[cell.ColIndex].Value.ToString(); // Add to undo stack var undo = new TextUndo(cell, cell.Text); List <IUndoRedoCmd> list = new List <IUndoRedoCmd>(); list.Add(undo); ss.AddUndo(new UndoRedoCollection(list, "text edit")); cell.Text = value; dataGrid.Rows[cell.RowIndex].Cells[cell.ColIndex].Value = cell.Value; updateUndoRedoText(); } }
/// <summary> /// Used to push the old text and updated text to the respective stacks in the UndoRedoCommandCollections /// -- BUG: when the value of a cell is changed by undoing text, it can not seem to redo that same text again, /// after it had been deleted by Undo /// </summary> /// <param name="targetCell"></param> /// <param name="row"></param> /// <param name="col"></param> /// <param name="prevText"></param> private void HandleUndoRedoText(Cell targetCell, int row, int col, string prevText) { Stack <Cell> newCells = new Stack <Cell>(); //updated Cell state Stack <Cell> oldCells = new Stack <Cell>(); //dataGridView1 cell state Stack <string> newText = new Stack <string>(); //updated Cell.Text property Stack <string> oldText = new Stack <string>(); //dataGridView1 cell text //create BGColor command for undo/redo SpreadsheetEngine.UndoRedoTextCommand commandText = new SpreadsheetEngine.UndoRedoTextCommand("Text"); oldText.Push(prevText); oldCells.Push(targetCell); Cell newCell = spreadSheet.GetCell(row, col); //get updated cell text newCells.Push(newCell); newText.Push(newCell.Text); commandText.SetCollections(oldText, newText, oldCells, newCells); spreadSheet.AddUndo(commandText); }
// fire when the user stops editing the cell private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { // get the row and column of the cell that we need to update int cellRow = e.RowIndex; int cellColumn = e.ColumnIndex; // get the actual cell Cell cellToUpdate = m_spreadsheet.GetCell(cellRow, cellColumn); // boolean to check whether the cell's text // was actually changed (i.e. the user clicked // into the cell then clicked out without // changing anything bool checkEdit = true; // create a RestoreText ICmd for the text change RestoreText[] undoText = new RestoreText[1]; // store the old text of the cell for a potential undo string oldText = cellToUpdate.Text; // instantiate the RestoreText with the oldText undoText[0] = new RestoreText(cellToUpdate, oldText); if (cellToUpdate != null) { // check to see if the user deleted the text of a cell try { // if the cell's text didn't change but there was text in the cell if (cellToUpdate.Text == dataGridView1.Rows[cellRow].Cells[cellColumn].Value.ToString()) { checkEdit = false; } // update the Text property of the cell to notify subscribers cellToUpdate.Text = dataGridView1.Rows[cellRow].Cells[cellColumn].Value.ToString(); } catch (NullReferenceException) { // if the cell didn't have text before and after the edit if (cellToUpdate.Text == null) { checkEdit = false; } cellToUpdate.Text = ""; } // update that cell in the spreadsheet to display its Value property dataGridView1.Rows[cellRow].Cells[cellColumn].Value = cellToUpdate.Value; // only add an undo if the cell was actually edited if (checkEdit == true) { // add the text change to the undo stack m_spreadsheet.AddUndo(new MultiCmd(undoText, "cell text change")); // update the edit menu options to display correctly UpdateEditMenu(); } } }