Beispiel #1
0
        private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            List <string> undoEntree = new List <string>();

            try
            {
                DataGridView    dgv        = sender as DataGridView;
                SpreadSheetCell editedCell = this.sheet.GetCell(e.RowIndex + 1, e.ColumnIndex);
                if (dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
                {
                    undoEntree.Add(e.RowIndex.ToString());
                    undoEntree.Add(e.ColumnIndex.ToString());
                    undoEntree.Add("txt");
                    undoEntree.Add(editedCell.Text);
                    editedCell.Text = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                    this.sheet.AddUndo(undoEntree);
                }
                else
                {
                    editedCell.Text = string.Empty;
                }

                dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = editedCell.ValueStr;
            }
            catch (IndexOutOfRangeException ex)
            {
                MessageBox.Show(ex.Message, "Index Out Of Range Error", MessageBoxButtons.OK);
            }
        }
Beispiel #2
0
        /// <summary>
        ///  This is the event function for if a cell of a spreadsheet changes.
        /// </summary>
        private void CellPropertyChanging(object sender, PropertyChangedEventArgs e)
        {
            SpreadSheetCell eventCell = sender as SpreadSheetCell;

            this.dataGridView1.Rows[eventCell.RowIndex].Cells[eventCell.ColumnIndex].Value           = eventCell.ValueStr;
            this.dataGridView1.Rows[eventCell.RowIndex].Cells[eventCell.ColumnIndex].Style.BackColor = this.UIntToColor(eventCell.Color);
        }
Beispiel #3
0
        /// <summary>
        /// Determines if a self reference exists.
        /// </summary>
        /// <param name="expression">
        /// The cells expression.
        /// </param>
        /// <param name="comparedCell">
        /// The spreadsheet cell used to determine if a self reference occured.
        /// </param>
        /// <returns>
        /// A bool to determine if a self reference has occured.
        /// </returns>
        private bool DetermineSelfReference(string expression, SpreadSheetCell comparedCell)
        {
            bool   result    = false;
            int    tempCol   = 0;
            string rowString = string.Empty;

            if (expression == string.Empty)
            {
                return(false);
            }

            for (int textIndex = 0; textIndex < expression.Length; textIndex++)
            {
                if (this.columnSymbols.Contains(expression[textIndex]))
                {
                    if (textIndex + 2 < expression.Length)
                    {
                        if (char.IsNumber(expression[textIndex + 1]))
                        {
                            if (char.IsNumber(expression[textIndex + 2]))
                            {
                                tempCol   = expression[textIndex] - 65;
                                rowString = expression[textIndex + 1].ToString() + expression[textIndex + 2].ToString();
                                if (this.GetCell(int.Parse(rowString), tempCol) == comparedCell)
                                {
                                    return(true);
                                }
                            }
                            else
                            {
                                tempCol   = expression[textIndex] - 65;
                                rowString = expression[textIndex + 1].ToString();
                                if (this.GetCell(int.Parse(rowString), tempCol) == comparedCell)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                    else
                    {
                        tempCol   = expression[textIndex] - 65;
                        rowString = expression[textIndex + 1].ToString();

                        if (this.GetCell(int.Parse(rowString), tempCol) == comparedCell)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Load the spreadsheet.
        /// </summary>
        /// <param name="infile">
        /// The selected xml file.
        /// </param>
        public void Load(Stream infile)
        {
            // XDocument is abstract, so obtain reference to it and laod infile.
            XDocument xmlReader = XDocument.Load(infile);

            foreach (XElement tag in xmlReader.Root.Elements("SpreadSheetCell"))
            {
                SpreadSheetCell cell = this.GetCell(int.Parse(tag.Element("cellrow").Value) + 1, int.Parse(tag.Element("columnrow").Value));
                cell.Text  = tag.Element("celltext").Value;
                cell.Color = uint.Parse(tag.Element("color").Value);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Determines if a cell is at default or not.
        /// </summary>
        /// <param name="inputCell">
        /// The examined cell.
        /// </param>
        /// <returns>
        /// A 1 or 0 integer to determine if it is at default.
        /// </returns>
        public int DetermineDefault(SpreadSheetCell inputCell)
        {
            if (inputCell.Text != string.Empty)
            {
                return(0);
            }
            else if (inputCell.Color != 0xFFFFFFFF)
            {
                return(0);
            }

            return(1);
        }
Beispiel #6
0
        /// <summary>
        /// Event handler for the change color menue item.
        /// </summary>
        /// <param name="sender">
        /// The sender object.
        /// </param>
        /// <param name="e">
        /// The events parameters.
        /// </param>
        private void ChangeColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List <string> undoEntree = new List <string>();
            ColorDialog   dlg        = new ColorDialog();

            dlg.ShowDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (DataGridViewCell cell in this.dataGridView1.SelectedCells)
                {
                    SpreadSheetCell editedCell = this.sheet.GetCell(cell.RowIndex + 1, cell.ColumnIndex);
                    undoEntree.Add(cell.RowIndex.ToString());
                    undoEntree.Add(cell.ColumnIndex.ToString());
                    undoEntree.Add("color");
                    undoEntree.Add(editedCell.Color.ToString());
                    editedCell.Color = this.ColorToUInt(dlg.Color);
                }

                this.sheet.AddUndo(undoEntree);
            }
        }
Beispiel #7
0
        /// <summary>
        /// This is event handler for spreedsheet when a property is changed.
        /// </summary>
        /// <param name="sender">
        /// Represents the cell object being changed.
        /// </param>
        /// <param name="e">
        /// Represents the event arguements.
        /// </param>
        public void CellPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            SpreadSheetCell eventCell = sender as SpreadSheetCell;

            if (eventCell.Text.Length == 0)
            {
                eventCell.ValueStr = string.Empty;
            }
            else if (eventCell.Text.StartsWith("="))
            {
                int    ifSelf = 0;
                string text   = eventCell.Text.Substring(1);
                this.spreadSheetTree = new ExpressionTree(text);
                string variableStr       = string.Empty;
                string variableStrParser = string.Empty;
                double declaredValue     = 0.0;
                int    tempCol           = 0;
                int    tempRow           = 0;
                string combiner          = string.Empty;
                if (this.DetermineBadReference(text) == true)
                {
                    eventCell.ValueStr = "!bad reference";
                }
                else if (this.DetermineSelfReference(text, eventCell) == true)
                {
                    eventCell.ValueStr = "!self reference";
                    ifSelf             = 1;
                }
                else
                {
                    if (text[text.Length - 1] == ')')
                    {
                        text = text.Substring(0, text.Length - 1);
                    }

                    for (int textIndex = 0; textIndex < text.Length; textIndex++)
                    {
                        if (this.columnSymbols.Contains(text[textIndex]))
                        {
                            if (textIndex + 2 == text.Length)
                            {
                                variableStr = text.Substring(textIndex);
                                tempCol     = variableStr[0] - 65;
                                tempRow     = variableStr[1] - 49;
                                variableStr = variableStr.Substring(0, 2);
                                try
                                {
                                    if (this.GetCell(tempRow + 1, tempCol) == eventCell)
                                    {
                                        eventCell.ValueStr = "!self reference";
                                        ifSelf             = 1;
                                        break;
                                    }
                                    else
                                    {
                                        declaredValue = Convert.ToDouble(this.GetCell(tempRow + 1, tempCol).ValueStr);
                                    }
                                }
                                catch (Exception)
                                {
                                    declaredValue = 0.0;
                                }

                                this.spreadSheetTree.SetVariable(variableStr, declaredValue);
                                this.GetCell(tempRow + 1, tempCol).DependencyChanged += eventCell.OnDependencyChanged;
                            }
                            else if (textIndex + 3 == text.Length)
                            {
                                variableStr       = text.Substring(textIndex);
                                tempCol           = variableStr[0] - 65;
                                variableStrParser = variableStr.Substring(1);
                                int.TryParse(variableStrParser, out tempRow);
                                tempRow--;
                                try
                                {
                                    if (this.GetCell(tempRow + 1, tempCol) == eventCell)
                                    {
                                        eventCell.ValueStr = "!self reference";
                                        ifSelf             = 1;
                                        break;
                                    }
                                    else
                                    {
                                        declaredValue = Convert.ToDouble(this.GetCell(tempRow + 1, tempCol).ValueStr);
                                    }
                                }
                                catch (Exception)
                                {
                                    declaredValue = 0.0;
                                }

                                this.spreadSheetTree.SetVariable(variableStr, declaredValue);
                                this.GetCell(tempRow + 1, tempCol).DependencyChanged += eventCell.OnDependencyChanged;
                            }
                            else
                            {
                                variableStr = text.Substring(textIndex);
                                if (this.operations.Contains(variableStr[2]))
                                {
                                    tempCol  = variableStr[0] - 65;
                                    tempRow  = variableStr[1] - 49;
                                    combiner = variableStr[0].ToString() + variableStr[1].ToString();
                                    try
                                    {
                                        if (this.GetCell(tempRow + 1, tempCol) == eventCell)
                                        {
                                            eventCell.ValueStr = "!self reference";
                                            ifSelf             = 1;
                                            break;
                                        }
                                        else
                                        {
                                            declaredValue = Convert.ToDouble(this.GetCell(tempRow + 1, tempCol).ValueStr);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        declaredValue = 0.0;
                                    }

                                    this.spreadSheetTree.SetVariable(combiner, declaredValue);
                                    this.GetCell(tempRow + 1, tempCol).DependencyChanged += eventCell.OnDependencyChanged;
                                }
                                else
                                {
                                    tempCol           = variableStr[0] - 65;
                                    variableStrParser = variableStr.Substring(1, 2);
                                    int.TryParse(variableStrParser, out tempRow);
                                    tempRow--;
                                    combiner = variableStr[0].ToString() + variableStr[1].ToString() + variableStr[2].ToString();
                                    try
                                    {
                                        if (this.GetCell(tempRow + 1, tempCol) == eventCell)
                                        {
                                            eventCell.ValueStr = "!self reference";
                                            ifSelf             = 1;
                                            break;
                                        }
                                        else
                                        {
                                            declaredValue = Convert.ToDouble(this.GetCell(tempRow + 1, tempCol).ValueStr);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        declaredValue = 0.0;
                                    }

                                    this.spreadSheetTree.SetVariable(combiner, declaredValue);
                                    this.GetCell(tempRow + 1, tempCol).DependencyChanged += eventCell.OnDependencyChanged;
                                }
                            }
                        }
                    }

                    if (ifSelf == 0 && this.DetermineCircularReference(text, eventCell) == true)
                    {
                        eventCell.ValueStr = "!circular reference";
                    }
                    else if (ifSelf == 0)
                    {
                        eventCell.ValueStr = this.spreadSheetTree.Evaluate().ToString();
                    }
                }
            }
            else
            {
                eventCell.ValueStr = eventCell.Text;
            }

            this.PropertyChanged?.Invoke(sender, new PropertyChangedEventArgs("ValueStr"));
        }