Ejemplo n.º 1
0
        /// <summary>
        /// A private Event Handler that is called whenever the ChangeContents event is fired.
        /// </summary>
        private void HandleChangeContents()
        {
            // Obtain the name of the currently selected cell and its desired contents.
            window.GetSelection(out int col, out int row);
            string        cellName   = toCellName(col, row);
            string        contents   = window.GetDesiredContents();
            ISet <string> dependents = new HashSet <string>();

            // Try and change the contents of the spreadsheet to the new contents.
            try
            {
                dependents = spreadsheet.SetContentsOfCell(cellName, contents);
            }
            catch (Exception e)
            {
                // Show the appropriate error message if the passed in contents aren't allowed.
                if (e is FormulaFormatException)
                {
                    window.InvalidFormula();
                }
                else if (e is CircularException)
                {
                    window.CircularFormula();
                }
                return;
            }

            // Update all of the cells so they show the correct value
            foreach (string s in dependents)
            {
                object value = spreadsheet.GetCellValue(s);
                toCoordinates(s, out int currentCol, out int currentRow);
                if (value is FormulaError)
                {
                    value = "#ERROR";
                }
                window.SetValue(currentCol, currentRow, value.ToString());
            }

            // Update the text box to show the correct contents.
            HandleNewCellSelected();
        }