/// <summary> /// Event when "File -> Open" is clicked. /// </summary> /// <param name="sender">Open Button</param> /// <param name="e">Open button click event</param> private void OpenMenuItem_Click(object sender, EventArgs e) { try { OpenFileDialog open = new OpenFileDialog(); open.Filter = "Spreadsheet Files (*.sprd) | *.sprd |All files (*.*) | *.*"; if (open.ShowDialog() == DialogResult.OK) { string file = open.FileName; this.spreadsheet = new Spreadsheet(file, s => true, s => s.ToUpper(), "six"); this.grid_widget.Clear(); //If the spreadsheet has been saved, then there should be cells used, which need to be gathered to fill in to the new spreadsheet. IEnumerable <string> usedCells = spreadsheet.GetNamesOfAllNonemptyCells(); GetCellCoordsAndSet(usedCells); //The given file that was opened is now the new saveFilePath for the spreadsheet GUI. saveFilePath = file; spreadsheet.Save(saveFilePath); } } catch (SpreadsheetReadWriteException) { MessageBox.Show("Invalid file type."); } }
private void newToolStripMenuItem_Click_1(object sender, EventArgs e) { HashSet <string> nonempty = new HashSet <string>(); foreach (string s in sheet.GetNamesOfAllNonemptyCells()) { nonempty.Add(s); } foreach (string s in nonempty) { sheet.SetContentsOfCell(s, string.Empty); } this.Enabled = false; this.Hide(); this.WindowState = FormWindowState.Minimized; this.Invoke(new MethodInvoker(() => Program.ConnectForm.Visible = true)); this.Invoke(new MethodInvoker(() => Program.ConnectForm.Enabled = true)); this.Invoke(new MethodInvoker(() => Program.ConnectForm.Show())); this.Invoke(new MethodInvoker(() => Program.ConnectForm.Focus())); this.Invoke(new MethodInvoker(() => Program.ConnectForm.WindowState = FormWindowState.Normal)); Model.Model.Unfocus(); spreadsheetPanel1.Clear(); this.spreadsheetPanel1.Invalidate(); }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { // Displays an OpenFileDialog so the user can select a Cursor. OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Spreadsheet File|*.sprd|All Files|*"; openFileDialog1.Title = "Open a Spreadsheet File"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string filename = openFileDialog1.FileName; if (sheet.GetSavedVersion(filename) != "ps6") { MessageBox.Show("The version of this spreadsheet file does not match \"ps6\"."); return; } currFileName = filename; sheet = new Spreadsheet(filename, s => true, s => s.ToUpper(), "ps6"); foreach (String cellName in sheet.GetNamesOfAllNonemptyCells()) { updateCell(cellName, spreadsheetPanel1); } spreadsheetPanel1.SelectionChanged += displaySelection; spreadsheetPanel1.SetSelection(0, 0); displaySelection(spreadsheetPanel1); } }
/// <summary> /// Controller constructor for opening file. /// </summary> public Controller(ISpreadsheetGUI window, string filename) { this.window = window; TextReader reader = new StreamReader(filename); spreadsheet = new Spreadsheet(reader, new Regex(@"^[a-zA-Z]+[1-9][0-9]*$")); reader.Close(); save = filename; foreach (string s in spreadsheet.GetNamesOfAllNonemptyCells()) { window.Update(s, spreadsheet.GetCellValue(s).ToString()); } string CellName = (((char)('A')).ToString()) + (1).ToString(); window.CellName = CellName; window.CellValue = spreadsheet.GetCellValue(CellName).ToString(); window.CellContents = spreadsheet.GetCellContents(CellName).ToString(); window.NewEvent += HandleNew; window.CloseEvent += HandleClose; window.KeyDownEvent += HandleKeyDown; window.SelectionChangeEvent += HandleSelectionChange; window.ContentsChangeEvent += HandleContentsChange; window.SaveCheckEvent += HandleSaveCheck; window.OpenEvent += HandleOpen; window.SaveEvent += HandleSave; window.SavingEvent += HandleSaving; }
/// <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; }
public void TestGetNamesOfAllNonemptyCells() { AbstractSpreadsheet s = buildSheet(); // Remove some content s.SetContentsOfCell("A1", ""); s.SetContentsOfCell("B1", ""); s.SetContentsOfCell("B2", ""); s.SetContentsOfCell("C1", ""); IEnumerator <string> e = s.GetNamesOfAllNonemptyCells().GetEnumerator(); e.MoveNext(); string val1 = e.Current; e.MoveNext(); string val2 = e.Current; e.MoveNext(); string val3 = e.Current; Assert.IsTrue(val1 == "A3" || val1 == "B3" || val1 == "C2"); Assert.IsTrue(val2 == "A3" || val2 == "B3" || val2 == "C2"); Assert.IsTrue(val3 == "A3" || val3 == "B3" || val3 == "C2"); }
//Open file private void openToolStripMenuItem_Click(object sender, EventArgs e) { //makes the spreadsheet have the values of the saved file. //have to figure out how to let the user choose a file. // Referenced MSDN library for Filter syntax openFileDialog1.Filter = "Spreadsheet Files (*.sprd)|*.sprd|All Files (*.*)|*.*"; openFileDialog1.DefaultExt = ".sprd"; openFileDialog1.FileName = ""; openFileDialog1.Title = "Open"; openFileDialog1.ShowDialog(); // get the name of the selected file string nameOfFile = openFileDialog1.FileName; //if the filename is empty, do nothing if (nameOfFile == "") { return; } // if i can't open that file type, explain why if (openFileDialog1.DefaultExt != "sprd") { MessageBox.Show("Unable to open file of Type " + openFileDialog1.DefaultExt); return; } //creates error message if trying to leave when there's a change. if (cells.Changed) { DialogResult result = MessageBox.Show("Your spreadsheet has unsaved changed. Opening " + "this file will overwrite any changes that have not been saved. Open anyways?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); // if 'No' then we don't want to open the file so return if (result == DialogResult.No) { return; } } spreadsheetPanel1.Clear(); cells = new Spreadsheet(nameOfFile, cells.IsValid, cells.Normalize, cells.Version); // updates the visual information/graphics to display the cell info. foreach (string name in cells.GetNamesOfAllNonemptyCells()) { // gets the column and row of the cell int col = revColumn[name.Substring(0, 1)]; int row = Int32.Parse(name.Substring(1)) - 1; // sets the value of the cell spreadsheetPanel1.SetValue(col, row, cells.GetCellValue(name).ToString()); } //sets the textbox value as the selected cell string name2 = column[prevCol] + (prevRow + 1); toolStripTextBox1.Text = cells.GetCellContents(name2).ToString(); }
// Every time the selection changes, this method is called with the // Spreadsheet as its parameter. /// <summary> /// This function will display the panels when the file is first opened. /// </summary> /// <param name="ss"></param> private void DisplayPanelOnOpen(SpreadsheetPanel ss) { // Looping through all of the non empty cells foreach (string cell in spread.GetNamesOfAllNonemptyCells()) { // setting the display of the cells to the empty cells. int cellCol = cell[0]; cellCol -= 65; string cellRowStr = cell.Substring(1); int.TryParse(cellRowStr, out int cellRow); cellRow -= 1; ss.SetValue(cellCol, cellRow, GetCellValueAsString(cell)); } }
public void TestCreateCellWithNumbers1() { for (int i = 1; i < 10; i++) { s.SetCellContents("A" + i, i); } List <object> list = new List <object>(); foreach (object t in s.GetNamesOfAllNonemptyCells()) { list.Add(t); } Assert.IsTrue(list.Count == 9); }
/// <summary> /// Returns the coordinates of all the nonempty cells in a spreadsheet /// </summary> /// <returns></returns> public HashSet <Tuple <int, int> > GetNonEmptyCells() { //contains the coordinates in the spreadsheet HashSet <Tuple <int, int> > coordinates = new HashSet <Tuple <int, int> >(); LinkedList <string> cellNames = (LinkedList <string>)ss.GetNamesOfAllNonemptyCells(); //For each non empty cell foreach (string cell in cellNames) { //Save the coordinates in a tuple and save the tuples to hashset VarToDigit(cell, out int col, out int row); coordinates.Add(new Tuple <int, int> (col, row)); } return(coordinates); }
/// <summary> /// helper method that redraws the spread sheet panel /// </summary> private void updateSpreadsheetGUI() { //save the current cell because method below might change these int saveRow = row; int saveCol = col; foreach (string cell in SSModel.GetNamesOfAllNonemptyCells()) { setRowAndCol(cell); spreadsheetPanel1.SetValue(col, row, SSModel.GetCellValue(cell).ToString()); } //done updating spreading sheet set row and col to //intial values row = saveRow; col = saveCol; }
/// <summary> /// Opens a previously saved document /// </summary> private void open_Click(object sender, EventArgs e) { int num = 1; if (ss.Changed) //check if current document has been modified { saveCheck(out num); } if (num == 0) { save_Click(sender, e); num = 1; } //user wishes to save if (num == 1) //open the document { try{ openFileDialog1.Filter = "Spreadsheet Files (.ss)|*.ss|All Files (*.*)|*.*"; openFileDialog1.FilterIndex = 1; openFileDialog1.FileName = ""; DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Test result. { string file = openFileDialog1.FileName; using (TextReader open = new StreamReader(file)) { ss = new Spreadsheet(open); cells.Clear(); int col, row; foreach (string s in ss.GetNamesOfAllNonemptyCells()) //Calculate all the cell values { getColRow(s, out col, out row); cells.SetValue(col, row, ss.GetCellValue(s).ToString()); } docName.Text = System.IO.Path.GetFileNameWithoutExtension(file); //set document name to opened file name ssName = file; savedas = true; } docName_TextChanged(sender, e); } SelectionChanged(cells); } //Error Message\\ catch (SpreadsheetReadException) { MessageBox.Show("The file you tried to open may be corrupt"); } } }
/// <summary> /// Creates a new PS6 spreadsheet form with the given file path /// </summary> /// <param name="filePath"></param> public PS6(string filePath) { InitializeComponent(); try { spreadsheet = new Spreadsheet(filePath, isValid, s => s.ToUpper(), "PS6"); } catch (Exception) { MessageBox.Show("File could not be read, check filename/extension/version/contents"); } this.spreadsheetPanel1.SelectionChanged += onCellClicked; this.AcceptButton = EnterButton; saveFileName = filePath; DisplayCellValues(spreadsheet.GetNamesOfAllNonemptyCells(), spreadsheetPanel1); }
public Form1(Spreadsheet sheet) { InitializeComponent(); this.sheet = sheet; foreach (String cellName in sheet.GetNamesOfAllNonemptyCells()) { updateCell(cellName, spreadsheetPanel1); } spreadsheetPanel1.SelectionChanged += displaySelection; spreadsheetPanel1.SetSelection(0, 0); displaySelection(spreadsheetPanel1); spreadsheetPanel1.SendToBack(); spreadsheetPanel1.Focus(); ContentField.BringToFront(); ContentField.Focus(); }
/// <summary> /// Helper method to update all cells that are non-empty /// </summary> private void update_NonEmpty_Cells() { IEnumerable <string> NonEmptyCells = spreadsheet.GetNamesOfAllNonemptyCells(); foreach (string cell in NonEmptyCells) { string colString = cell[0].ToString(); string rowString = cell.Substring(1); int col = letters.IndexOf(colString); int row; int.TryParse(rowString, out row); row -= 1; update_content_textbox(col, row); update_name_textbox(col, row); update_value_textbox(col, row); } }
/// <summary> /// Handles request to open file /// </summary> private void HandleFileChosen(string filename) { try { TextReader read = File.OpenText(filename); spreadsheet = new Spreadsheet(read); window.Title = filename; ClearSheet(); foreach (string s in spreadsheet.GetNamesOfAllNonemptyCells()) { setCell(s); } RefreshTextBoxes(); read.Close(); } catch (Exception e) { window.Message = "Unable to open file\n" + e.Message; } }
/// <summary> /// deals with open a new file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openToolStripMenuItem_Click(object sender, EventArgs e) { //check if orignal file has been changed if so ask if they want to save. String message = "Would you like to save before closing?"; var result1 = MessageBox.Show(message, "Closing", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result1 == DialogResult.Yes) { saveFile(); } //With help from www.dotnetperls.com //show dialog DialogResult result = openFileDialog1.ShowDialog(); //check result if (result == DialogResult.OK) { string fileName = openFileDialog1.FileName; try { //make new spreadsheet SSModel = new Spreadsheet(fileName, s => Regex.IsMatch(s, @"^[a-zA-Z][1-9][0-9]?$"), s => s.ToUpper(), "ps6"); //fill in the cells in the GUI foreach (String cellName in SSModel.GetNamesOfAllNonemptyCells()) { setRowAndCol(cellName); spreadsheetPanel1.SetValue(col, row, SSModel.GetCellValue(cellName).ToString()); } //set up the spreadsheet spreadsheetPanel1.SetSelection(0, 0); displayCurrentCell(0, 0); }catch (Exception ex) { MessageBox.Show("An error occured:\n " + ex.Message); } } }