public Spreadsheet(int rows, int columns) { RowCount = rows; ColumnCount = columns; sheet = new newCell[rows, columns]; //create inner spreadsheet for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { var cell = new newCell(i, j); //create new cell sheet[i, j] = cell; //assign to array position for inner spreadsheet sheet[i, j].PropertyChanged += Spreadsheet_PropertyChanged; //subscribe to event } } }
private string CalculateValue(newCell cell) { bool invalidRef = false; if (cell.Text[0] != '=') //check for function { cell.SetValue(cell.Text); return(cell.Value); } if (cell.getTree() != null) // remove old dependencies when new tree is created { foreach (var variable in cell.getTree().getVarList()) { if (variable.Length >= 2 && char.IsDigit(variable[1])) { cell.unsubscribe(GetCell(variable) as newCell); } } } //remove "=" var expression = cell.Text.Substring(1); cell.setTree(new ExpTree(expression)); foreach (var variable in cell.getTree().getVarList()) { // check for valid column entry if (variable.Length >= 2 && char.IsDigit(variable[1])) { cell.subscribe(GetCell(variable) as newCell); cell.getTree().SetVar(variable, Convert.ToDouble(GetCell(variable).Value)); } else //bad cell reference { invalidRef = true; } } if (invalidRef) { cell.SetValue("#REF"); } else { cell.SetValue(cell.getTree().Eval().ToString()); } return(cell.Value); }
public void subscribe(newCell reference) { reference.PropertyChanged += ReEval; }