/// <summary> /// Event when the paste option is clicked on the right-click menu. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pasteToolStripMenuItem_Click(object sender, EventArgs e) { // If nothing has been copied, do nothing. if (CopyContents.ToString() == "") { return; } // Otherwise, get coordinates and name of selected cell. SpreadSheetPanel.GetSelection(out CurrentColumn, out CurrentRow); CurrentCellName = GetCellName(CurrentColumn, CurrentRow); // If copied cell was a formula, append an = to the beginning of the content string. if (CopyContents is Formula) { Spreadsheet.SetContentsOfCell(CurrentCellName, "=" + CopyContents.ToString()); } // Otherwise simply add the copied contents to the cell. else { Spreadsheet.SetContentsOfCell(CurrentCellName, CopyContents.ToString()); } // Update cells and selection values. Update(); GetSelectionValues(SpreadSheetPanel); }
/// <summary> /// Opens spreadsheet from file, current spreadsheet will be closed. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openToolStripMenuItem_Click(object sender, EventArgs e) { // Create file dialog window and add filter. OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "sprd files (*.sprd)|*sprd| All files (*.*)|*.*"; // Store result of dialog choice. DialogResult Result = openFileDialog.ShowDialog(); // If OK was pressed, attempt to save file to the specified location. If an exception occurs, display an error message and exit. if (Result == DialogResult.OK) { try { SpreadSheetPanel.Clear(); Spreadsheet = new Spreadsheet(openFileDialog.FileName, Validator, Normalizer, "ps6"); GetSelectionValues(SpreadSheetPanel); Update(); } catch (Exception) { MessageBox.Show(null, "Error reading file.", "Error"); } } // If cancel is pressed, simply return to spreadsheet. else if (Result == DialogResult.Cancel) { return; } }
/// <summary> /// Updates the values of all nonempty cells in the spreadsheet. Used to recalculate formulas when cells are changed. /// </summary> private void UpdateCells() { // Store coordinates of selection to restore at end of updating process. int StartRow = CurrentRow; int StartColumn = CurrentColumn; foreach (string name in Spreadsheet.GetNamesOfAllNonemptyCells()) { // Get value of current cell object CellValue = Spreadsheet.GetCellValue(name); // If a formula has broken, a message is displayed and the formula is removed. if (CellValue is FormulaError) { MessageBox.Show(null, "Deletion has caused a formula to break.", "Error"); } // Get spreadsheetpanel coordinates for current cell. GetCoordsFromName(name); // Update each cell with its new value. try { SpreadSheetPanel.SetValue(CurrentColumn, CurrentRow, CellValue.ToString()); } // If current cell is empty when method is called a NRE will be thrown, in this case, move to the next cell. catch (NullReferenceException) { continue; } } // Restore initial coordinates. CurrentRow = StartRow; CurrentColumn = StartColumn; }
/// <summary> /// Event when delete option is clicked on the right-clikc menu. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { // Get selected cell's coordinates and name. SpreadSheetPanel.GetSelection(out CurrentColumn, out CurrentRow); Spreadsheet.SetContentsOfCell(CurrentCellName, ""); // Set the displayed value and contents to "" SpreadSheetPanel.SetValue(CurrentColumn, CurrentRow, ""); CellContentsField.Text = ""; // Update cells and selection values. Update(); GetSelectionValues(SpreadSheetPanel); }
/// <summary> /// Event when the copy option is clicked on the right-click menu. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void copyToolStripMenuItem_Click(object sender, EventArgs e) { // Get current selection's coordinates and name. SpreadSheetPanel.GetSelection(out CurrentColumn, out CurrentRow); CurrentCellName = GetCellName(CurrentColumn, CurrentRow); // Get contents of cell and store them. CopyContents = Spreadsheet.GetCellContents(CurrentCellName); // If cell is empty, the copy operation cannot be performed. Display error messgae. if (CopyContents.ToString() == "") { MessageBox.Show(null, "Error: Cannot copy empty cell.", "Error"); } // Store the cell's value as well. CopyValue = Spreadsheet.GetCellValue(CurrentCellName); }
/// <summary> /// Updates cell content/value when text is entered into the textbox and the enter key is pressed. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { // Get rid of ANNOYING DING e.SuppressKeyPress = true; // Store the previous contents of cell if later needed. string OldContents = Spreadsheet.GetCellContents(CurrentCellName).ToString(); // Try to set the contents of cell to the entered text. try { Spreadsheet.SetContentsOfCell(CurrentCellName, CellContentsField.Text); } // Supposed to catch argument exception (which is supposed to cause formula error in formula class), something is wrong here sadly :(((((( catch (ArgumentException) { MessageBox.Show(null, "Error, error in formula calculation.", "Error"); Spreadsheet.SetContentsOfCell(CurrentCellName, OldContents); CellContentsField.Text = OldContents; return; } // If an invalid formula is entered, an error message pops up and the cell returns to its original contents. catch (FormulaFormatException) { MessageBox.Show(null, "Error, formula format is invalid.", "Error"); Spreadsheet.SetContentsOfCell(CurrentCellName, OldContents); CellContentsField.Text = OldContents; return; } // If a circular exception is encountered, an error message is show and the cell returns to its original contents. catch (CircularException) { MessageBox.Show(null, "Error, circular dependcy exists, check your formulas.", "Error"); Spreadsheet.SetContentsOfCell(CurrentCellName, OldContents); CellContentsField.Text = OldContents; return; } // Get the value of the current cell in the spreadsheet. object CellType = Spreadsheet.GetCellValue(CurrentCellName); // If a formula error was returned, an error message is shown and the cell returns to its original contents. if (CellType is FormulaError) { MessageBox.Show(null, "Error, formlua returns FormulaError.", "Error"); Spreadsheet.SetContentsOfCell(CurrentCellName, OldContents); CellContentsField.Text = OldContents; return; } string CellValue = ""; // If no errors are encountered, store the cell value as a string. try { CellValue = CellType.ToString(); } catch (NullReferenceException) { } // Update the spreadsheetpanel with the new value. SpreadSheetPanel.SetValue(CurrentColumn, CurrentRow, CellValue); // Update corresponding label. this.CellValueDisplayLabel.Text = CellValue; Update(); GetSelectionValues(SpreadSheetPanel); } }