/// <summary>
        /// Method for handling the opening of files when the Open button is clicked.
        /// When a file is opened, all previous data in the current spreadsheet is lost and replaced with the data from the file being opened.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Create new OpenFileDialog object
            OpenFileDialog openFile = new OpenFileDialog();

            // Set filter for which files can be shown
            openFile.Filter = "Spreadsheet File (*.ss)|*.ss|All Files |*.*";
            // Set default filter to .ss files
            openFile.FilterIndex = 1;
            openFile.Multiselect = false;

            // Try to open the file
            try
            {
                // If Open has been selected
                if (openFile.ShowDialog() == DialogResult.OK)
                {
                    // Read in the file
                    StreamReader reader = new StreamReader(openFile.FileName);
                    // Replace the old spreadSheet object with the new one created by the file opener
                    this.spreadSheet = new Spreadsheet(reader);
                    // Clear the spreadsheet panel to get the old cells off of it
                    spreadsheetPanel1.Clear();
                    // Update the fileName in the instance variable
                    currentFileName = openFile.FileName;

                    // Add all of the cells in the spreadsheet into the spreadsheet panel
                    IEnumerable <String> cells = spreadSheet.GetNamesOfAllNonemptyCells();
                    foreach (String cellName in cells)
                    {
                        // Parse cell's row and column information
                        String parsedCol = cellName.Substring(0, 1);
                        String parsedRow = cellName.Substring(1, cellName.Length - 1);

                        Char parsedChar = parsedCol[0];
                        int  col        = (int)parsedChar - 65;
                        int  row        = Convert.ToInt32(parsedRow) - 1;

                        // Add cell into the spreadsheet panel
                        spreadsheetPanel1.SetValue(col, row, spreadSheet.GetCellValue(cellName).ToString());

                        // Set the values for the default selected cell A1
                        cellContentsValue.Text = spreadSheet.GetCellContents("A1").ToString();
                        cellValueBox.Text      = spreadSheet.GetCellValue("A1").ToString();
                        spreadsheetPanel1.SetSelection(0, 0);
                    }

                    // Close the reader
                    reader.Close();
                }
            }
            // Catch any SpreadsheetReadExceptions and show the exception in a dialog box
            catch (SpreadsheetReadException rexception)
            {
                MessageBox.Show(rexception.Message);
                closeToolStripMenuItem_Click(sender, e);
            }
        }
Exemple #2
0
        /// <summary>
        /// Called when a cell on the spreadsheet is selected.
        /// </summary>
        /// <param name="ss">Spreadsheet that called the method.</param>
        private void cellSelected(SpreadsheetPanel ss)
        {
            int row, col;

            ss.GetSelection(out col, out row);
            String _cellName = "" + ((char)('A' + col)) + (row + 1);

            if (shiftPressed) //If shift is pressed we are only adding the cell name to the contents box.
            {
                int selectionStart = contentsBox.SelectionStart;
                contentsBox.Text = contentsBox.Text.Insert(selectionStart, _cellName);
                contentsBox.Select(selectionStart + _cellName.Length, 0);
                ss.SetSelection(previousCellX, previousCellY);
                return;
            }
            if (contentButton.Enabled) // If we click away submit what we've got if there has been a change.
            {
                contentButton_Click(null, null);
            }
            previousCellX = col;
            previousCellY = row;
            cellName.Text = _cellName;
            String contents = ourSpreadsheet.GetCellContents(_cellName).ToString();
            String value    = ourSpreadsheet.GetCellValue(_cellName).ToString();

            contentsBox.Text = contents;
            previousContents = contents;
            if (value == "SpreadsheetUtilities.FormulaError")
            {
                value = "!FORMULA_ERROR"; //Change the name to something more readable
                if (heroMode)             // HERO MODE Stuff
                {
                    HeroModeReset();
                }
            }
            toolTip.SetToolTip(valueBox, value); //Set the tooltip to be the full value
            if (value.Length > 16)               // Trim it down to fit in the contents box
            {
                value = value.Substring(0, 15);
            }
            valueBox.Text = value;

            //Pretty Colorized stuff
            //colorizeDependencies(_cellName);

            contentButton.Enabled = false;//Keep the contents button disabled
        }
        /// <summary>
        /// Helper method for updating the cell's value boxes when the selection changes
        /// This method correctly sets the value textboxes: cellNameValue, columnValue, rowValue, cellContentsValue, and the cellValueBox textboxes
        /// </summary>
        /// <param name="sheet"></param>
        private void displaySelection(SpreadsheetPanel sheet)
        {
            // Create variables for holding the row, column, and cell values
            int    row;
            int    col;
            String value;

            // Get the current selection
            sheet.GetSelection(out col, out row);
            // Get the cell's value once the selection is obtained
            sheet.GetValue(col, row, out value);

            // Add ascii offset to the column number
            int asciiCol = col + 65;

            // Convert the ascii value into a char and set it as the columnValue text box
            columnValue.Text = Char.ConvertFromUtf32(asciiCol);

            // Add 1 to the row number since rows and columns starting index is 0
            int actualRow = row + 1;

            // Convert row number to a string and store it in the rowValue text box
            rowValue.Text = actualRow.ToString();

            // Combine the column and row values as a string and set them as the cellNameValue text box
            cellNameValue.Text = Char.ConvertFromUtf32(asciiCol) + actualRow.ToString();

            // Get the contents of the cell and convert it to a string
            String cellContents = spreadSheet.GetCellContents(cellNameValue.Text).ToString();

            // Set the cell's contents in the cellContentsValue text box
            cellContentsValue.Text = cellContents;

            // Get the vlaue of the cell and convert it to a string
            String cellValue = spreadSheet.GetCellValue(cellNameValue.Text).ToString();

            // Set the clel's value in the cellValueBox textbox
            cellValueBox.Text = cellValue;
        }