/// <summary> /// This method opens a file from a specified file path. It does this by creating a /// new spreadsheet with the specified contents and updating the visuals. /// </summary> /// <param name="FileName">Name of file being opened</param> private void OpenFile(string FileName) { //Clears the spreadsheet and updates the previous row/column SpreadsheetGrid.Clear(); transformedCol = transformedRow = 0; //Imports the spreadsheet and records the text. The title of the form is set equal to the file name. mainSpreadsheet = new Spreadsheet(FileName, SpreadsheetCellIsValid, SpreadsheetCellNormalizer, "ps6"); this.FileName = FileName; Text = Path.GetFileName(FileName); //Updates the visuals of each nonempty cell foreach (string cell in mainSpreadsheet.GetNamesOfAllNonemptyCells().ToList()) { int rowToChange = Int32.Parse(cell.Substring(1)) - 1; int letterToNumberCol = char.ToUpper(cell[0]) - 65; if (mainSpreadsheet.GetCellValue(cell) is FormulaError) { SpreadsheetGrid.SetValue(letterToNumberCol, rowToChange, "Formula Error"); } else { SpreadsheetGrid.SetValue(letterToNumberCol, rowToChange, mainSpreadsheet.GetCellValue(cell).ToString()); } } //Resets the selection and text boxes. SpreadsheetGrid.SetSelection(0, 0); VisualUpdate("A1"); }
/// <summary> /// Undo Method /// /// This method undoes one change made in the spreadsheet if the keys /// CONTROL and Z are clicked at the same time. Undoing will also only /// occur when the variable 'possibleToUndo' is true. /// /// The spreadsheet will select the previously selected cell and update its /// contents to its previous contents. Then re-update the cell entirely as /// to make sure dependencies are also updated. /// /// 'possibleToUndo' will be reset to false. So undoing is only possible once /// until another change is made. /// </summary> private void SpreadsheetWindow_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control && possibleToUndo) { string previousCellName = SelectedCellName(previousCol, previousRow); mainSpreadsheet.SetContentsOfCell(previousCellName, previousContents); SpreadsheetGrid.SetSelection(previousCol, previousRow); SetTextBoxesVisuals(previousCellName); UpdateCells(previousCol, previousRow); possibleToUndo = false; } }
/// <summary> /// Updates a specified cell and it's dependencies. /// /// If it's empty, it just returns out of this method. /// /// This also catches any exceptions that occur /// </summary> /// <param name="col">Column of the cell that's being updated</param> /// <param name="row">Row of the cell that's being updated</param> private void UpdateCells(int col, int row) { string cellName = SelectedCellName(col, row); try { IList <string> dependencies = mainSpreadsheet.SetContentsOfCell(cellName, CellContentsBox.Text); foreach (string cell in dependencies) { int rowToChange = Int32.Parse(cell.Substring(1)) - 1; int letterToNumberCol = char.ToUpper(cell[0]) - 65; //Sets the visuals to the string "Formula Error" if the value is a FormulaError if (mainSpreadsheet.GetCellValue(cell) is FormulaError) { SpreadsheetGrid.SetValue(letterToNumberCol, rowToChange, "Formula Error"); } else { SpreadsheetGrid.SetValue(letterToNumberCol, rowToChange, mainSpreadsheet.GetCellValue(cell).ToString()); } } SpreadsheetGrid.SetSelection(col, row); // Visual change in cell string cellValue = mainSpreadsheet.GetCellValue(cellName).ToString(); VisualUpdate(cellName); //Adds an asterisk to the title if the file has changed if (mainSpreadsheet.Changed) { Text = Path.GetFileName(FileName) + "*"; } } //Catches any exceptions while updating the value and creates a dialog to show it. catch { MessageBox.Show("An error occured while trying to change the contents of a cell", "Contents of Cell Error", MessageBoxButtons.OK); VisualUpdate(cellName); } }
public SpreadsheetColumnExt(SpreadsheetGrid grid, SfSpreadsheet spreadsheet) : base(grid) { this.grid = grid; this.spreadsheet = spreadsheet; }
/// <summary> /// This method handles any key inputs. /// -The Enter key updates the value of a cell. /// -The arrow keys update the value and then move the selection. /// -Ctrl + Z undos the last change that has been made. /// </summary> /// <param name="keyCode">Code of the key that's been pressed</param> /// <param name="keyModifier">Modifier of the key that's been pressed</param> private void HandleKeys(Keys keyCode, Keys keyModifier) { SpreadsheetGrid.GetSelection(out int col, out int row); //Enter Key if (keyCode == Keys.Enter) { SetUndo(); UpdateCells(col, row); } //Up Arrow Key if (keyCode == Keys.Up) { SetUndo(); UpdateCells(col, row); string cellName = SelectedCellName(col, row); //Only moves up if the row isn't at 0 if (row != 0) { transformedCol = col; transformedRow = row - 1; SpreadsheetGrid.SetSelection(col, row - 1); cellName = SelectedCellName(col, row - 1); } VisualUpdate(cellName); } //Down Arrow Key if (keyCode == Keys.Down) { SetUndo(); UpdateCells(col, row); string cellName = SelectedCellName(col, row); if (row != 98) { transformedCol = col; transformedRow = row + 1; SpreadsheetGrid.SetSelection(col, row + 1); cellName = SelectedCellName(col, row + 1); } VisualUpdate(cellName); } //Left Arrow Key if (keyCode == Keys.Left) { SetUndo(); UpdateCells(col, row); string cellName = SelectedCellName(col, row); if (col != 0) { transformedCol = col - 1; transformedRow = row; SpreadsheetGrid.SetSelection(col - 1, row); cellName = SelectedCellName(col - 1, row); } VisualUpdate(cellName); } //Right Arrow Key if (keyCode == Keys.Right) { SetUndo(); UpdateCells(col, row); string cellName = SelectedCellName(col, row); if (col != 25) { transformedCol = col + 1; transformedRow = row; SpreadsheetGrid.SetSelection(col + 1, row); cellName = SelectedCellName(col + 1, row); } VisualUpdate(cellName); } CellContentsBox.SelectionStart = CellContentsBox.Text.Length + 1; }