public void OnPropertyChanged(object sender, PropertyChangedEventArgs e) { // if property name is equal to text if (e.PropertyName == "Text") { InstanciateCell tCell = sender as InstanciateCell; DeleteDeps(tCell.Name); // update cell value to text //tCell.SetVal(tCell.Text); // if it starts with an equal sign if (tCell.Text != "" && tCell.Text[0] == '=' && tCell.Text.Length > 1) { ExpTree exp = new ExpTree(tCell.Text.Substring(1)); MakeDeps(tCell.Name, exp.getVars()); } EvalCell(sender as Cell); } else if (e.PropertyName == "BackColor") { CellPropertyChanged(sender, new PropertyChangedEventArgs("BackColor")); } }
private void EvalCell(Cell cell) { InstanciateCell mCell = cell as InstanciateCell; // if the string is empty if (string.IsNullOrEmpty(mCell.Text)) { mCell.SetVal(""); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } // if it an equation else if (mCell.Text[0] == '=' && mCell.Text.Length > 1) { // remove = string exp = mCell.Text.Substring(1); SpreadsheetEngine.ExpTree mExp = new SpreadsheetEngine.ExpTree(exp); string[] arrayVars = mExp.getVars(); foreach (string v in arrayVars) { if (GetCell(v) == null) { mCell.SetVal("Bad Ref"); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); break; } // attempt at setting variable to value SetExpVar(mExp, v); } mCell.SetVal(mExp.Eval().ToString()); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } // not an expression else { mCell.SetVal(mCell.Text); CellPropertyChanged(cell, new PropertyChangedEventArgs("Value")); } // evaluate dependencies if (depDict.ContainsKey(mCell.Name)) { foreach (string name in depDict[mCell.Name]) { Eval(name); } } }
// spreadsheet constructor public Spreadsheet(int rows, int cols) { cells = new Cell[rows, cols]; // loop through and create instantiation of cell for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cells[i, j] = new InstanciateCell(i, j); cells[i, j].PropertyChanged += OnPropertyChanged; } } }