// Multiple Cell Change Header Name Event Handler void mc_NotifyHeaderNameChange(object sender, EventArgs e) { // Find the multiple cell column label for the current row // need to update to use multiple multiple cells Cell c = sender as Cell; if (c == null) { return; } MultipleCell mc = null; // find multiple cell in column for (int i = 0; i < NewBalanceDataGridView.ColumnCount; i++) { if (ViewData.GetCell(c.RowIndex, i) is MultipleCell) { mc = ViewData.GetCell(c.RowIndex, i) as MultipleCell; } } // did not find a multiplecell, return if (mc == null) { return; } // don't update if the row is the same if (NewBalanceDataGridView.Columns[mc.ColumnIndex].HeaderText == mc.SelectedCell.Label) { return; } // got here, update the Label NewBalanceDataGridView.Columns[mc.ColumnIndex].HeaderText = mc.SelectedCell.Label; }
//Right Click Menu Event void t_Click(object sender, EventArgs e) { ToolStripMenuItem t = sender as ToolStripMenuItem; if (t == null) { return; } MultipleCell mc = null; // currently this only works for one multiple cell // need to refactor to include multiple multiple cells for (int i = 0; i < ViewData.ColumnHeaders().Count; i++) { if (ViewData.GetCellType(0, i) == CellType.Multiple) { mc = ViewData.GetCell(0, i) as MultipleCell; break; } } // make sure we have the multiple cell selected if (mc == null) { return; } int rowIndex = NewBalanceDataGridView.CurrentCell.RowIndex; mc = ViewData.GetCell(rowIndex, mc.ColumnIndex) as MultipleCell; if (mc == null) { return; } mc.ChangeOption(t.ToString()); }
//Add Row to Logic and GUI void AddRowToDataAndView() { int rowNumber = NewBalanceDataGridView.RowCount; // will be correct row index when we add a row NewBalanceDataGridView.Rows.Add(); ViewData.AddRow(); // prevent a bunch of event call updates and just update manually here for (int i = 0; i < NewBalanceDataGridView.ColumnCount; i++) { NewBalanceDataGridView.Rows[rowNumber].Cells[i].Value = ViewData.GetCell(rowNumber, i).Value; } for (int i = 0; i < NewBalanceDataGridView.ColumnCount; i++) { if (ViewData.GetCellType(rowNumber, i) == CellType.Multiple) { MultipleCell mc = ViewData.GetCell(rowNumber, i) as MultipleCell; mc.NotifyHeaderNameChange += mc_NotifyHeaderNameChange; mc.CellValueChanged += NewBalance_CellValueChanged; mc.NotifyDependents += NewBalance_NotifyDependents; foreach (var cell in mc.CellOptions) { cell.CellValueChanged += NewBalance_CellValueChanged; cell.NotifyDependents += NewBalance_NotifyDependents; } } else { Cell c = ViewData.GetCell(rowNumber, i); c.CellValueChanged += NewBalance_CellValueChanged; c.NotifyDependents += NewBalance_NotifyDependents; } } }
public List <Cell> ReturnCellsInRow(int rowNumber) { List <Cell> cols = new List <Cell>(); int colIndex = 0; foreach (List <string> l in _parsedFile) { MultipleCell multi = null; // there will be 5 items in each .APP file if (l.Count != 5) { break; } string label = l[0]; string type = l[1]; int digits; // not really necessary for this application, but leaving for reverse compatibility int.TryParse(l[2], out digits); int precision; int.TryParse(l[3], out precision); string connectionInfo = l[4]; switch (type) { // keyboard cell case "K": if (MultipleCell.IsMultiCell(label) && cols.OfType <MultipleCell>().ToList().Count > 0) { colIndex--; } KCell k = new KCell(label, digits, precision, connectionInfo, rowNumber, colIndex); if (MultipleCell.IsMultiCell(label)) { multi = BuildMultipleCell(k, cols); if (multi != null) { cols.Add(multi); } } else { cols.Add(k); } break; // weight cell case "W": if (MultipleCell.IsMultiCell(label) && cols.OfType <MultipleCell>().ToList().Count > 0) { colIndex--; } WCell w = new WCell(label, digits, precision, connectionInfo, rowNumber, colIndex); if (MultipleCell.IsMultiCell(label)) { multi = BuildMultipleCell(w, cols); if (multi != null) { cols.Add(multi); } } else { cols.Add(w); } break; // calculation cell case "C": List <string> names = connectionInfo.Split(new char[] { '(', ')', '+', '-', '*', '/', '^' }).ToList(); // need for TryParse double n; List <string> dependencyNamesPossibleDupes = (from name in names where name.Length > 0 && !double.TryParse(name, out n) // make sure the value isn't an integer select name).ToList(); List <string> dependencyNames = dependencyNamesPossibleDupes.Distinct().ToList(); List <Cell> dependencies = new List <Cell>(); foreach (string name in dependencyNames) { Cell dependency = cols.First(x => x.Label == name); if (dependency != null) { dependencies.Add(dependency); } } if (MultipleCell.IsMultiCell(label) && cols.OfType <MultipleCell>().ToList().Count > 0) { colIndex--; } CCell c = new CCell(label, digits, precision, connectionInfo, rowNumber, colIndex, dependencies); if (MultipleCell.IsMultiCell(label)) { multi = BuildMultipleCell(c, cols); if (multi != null) { cols.Add(multi); } } else { cols.Add(c); } break; // mirror cell case "M": // find cell to mirror var columnToMirror = cols.FirstOrDefault(x => x.Label == connectionInfo); if (MultipleCell.IsMultiCell(label) && cols.OfType <MultipleCell>().ToList().Count > 0) { colIndex--; } MCell m = new MCell(label, digits, precision, connectionInfo, rowNumber, colIndex, columnToMirror); if (MultipleCell.IsMultiCell(label)) { multi = BuildMultipleCell(m, cols); if (multi != null) { cols.Add(multi); } } else { cols.Add(m); } break; // newscreen, for backwards compatibility case "0": NewScreen newScreen = new NewScreen(label, digits, precision, connectionInfo, rowNumber, colIndex); cols.Add(newScreen); break; } colIndex++; } return(cols); }
//Clears GridView and Any Subscriptions private void EmptyGridView(string nameOfApp) { int index = App.GetFileNames().IndexOf(nameOfApp.ToLower()); if (index < 0) { throw new Exception("Cannot find file \"" + nameOfApp + ".app\" in " + Settings.Default.DefaultPath); } AppParse = new AppParser(App.GetDirectoryNames()[index]); if (ViewData != null) { for (int j = 0; j < NewBalanceDataGridView.RowCount; j++) { for (int i = 0; i < NewBalanceDataGridView.ColumnCount; i++) { if (ViewData.GetCellType(0, i) == CellType.Multiple) { MultipleCell mc = ViewData.GetCell(0, i) as MultipleCell; mc.NotifyHeaderNameChange -= mc_NotifyHeaderNameChange; mc.CellValueChanged -= NewBalance_CellValueChanged; mc.NotifyDependents -= NewBalance_NotifyDependents; foreach (var cell in mc.CellOptions) { cell.CellValueChanged -= NewBalance_CellValueChanged; cell.NotifyDependents -= NewBalance_NotifyDependents; } } else { Cell c = ViewData.GetCell(0, i); c.CellValueChanged -= NewBalance_CellValueChanged; c.NotifyDependents -= NewBalance_NotifyDependents; } } } // unfreeze any old frozen columns NewBalanceDataGridView.Columns[FindDisplayIndexColumnIndex(0)].Frozen = false; // make sure the actual index and viewindex are the same for (int i = 0; i < NewBalanceDataGridView.ColumnCount; i++) { NewBalanceDataGridView.Columns[i].DisplayIndex = i; } } NewBalanceDataGridView.Rows.Clear(); ViewData = new GridData(AppParse.ReturnCellsInRow(NewBalanceDataGridView.RowCount), nameOfApp); // update right click context menu if multiple cells exist RightClickMenu = new ContextMenuStrip(); NewBalanceDataGridView.ContextMenuStrip = RightClickMenu; if (ViewData.HasMultipleCells()) { for (int i = 0; i < ViewData.ColumnHeaders().Count; i++) { var cell = ViewData.GetCell(0, i) as MultipleCell; if (cell != null) { foreach (string s in cell.OptionStrings) { ToolStripMenuItem t = new ToolStripMenuItem(s); t.Click += t_Click; RightClickMenu.Items.Add(t); } break; } } } List <string> columnHeaders = ViewData.ColumnHeaders(); int count = columnHeaders.Count; NewBalanceDataGridView.ColumnCount = count; NewBalanceDataGridView.CellBeginEdit += NewBalanceDataGridView_CellBeginEdit; NewBalanceDataGridView.CellEndEdit += NewBalanceDataGridView_CellEndEdit; NewBalanceDataGridView.Rows.Add(); for (int i = 0; i < count; i++) { NewBalanceDataGridView.Columns[i].HeaderText = columnHeaders[i]; // hide newscreen cells (backwards compatibility) NewBalanceDataGridView.Columns[i].Visible = !(NewBalanceDataGridView.Columns[i].HeaderText.ToLower().Contains("newscreen")); // set maxium input size (backwards compatibility) ((DataGridViewTextBoxColumn)NewBalanceDataGridView.Columns[i]).MaxInputLength = ViewData.GetCell(0, i).Digits; NewBalanceDataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; NewBalanceDataGridView.Rows[0].Cells[i].Value = ViewData.GetCell(0, i).Value.ToString(); //// set default formatting options //NewBalanceDataGridView.Columns[i].DefaultCellStyle.Format = "N" + ViewData.GetCell(0, i).Precision; } for (int i = 0; i < NewBalanceDataGridView.Rows[0].Cells.Count; i++) { if (ViewData.GetCellType(0, i) == CellType.Multiple) { MultipleCell mc = ViewData.GetCell(0, i) as MultipleCell; mc.NotifyHeaderNameChange += mc_NotifyHeaderNameChange; mc.CellValueChanged += NewBalance_CellValueChanged; mc.NotifyDependents += NewBalance_NotifyDependents; foreach (var cell in mc.CellOptions) { cell.CellValueChanged += NewBalance_CellValueChanged; cell.NotifyDependents += NewBalance_NotifyDependents; } } else { Cell c = ViewData.GetCell(0, i); c.CellValueChanged += NewBalance_CellValueChanged; c.NotifyDependents += NewBalance_NotifyDependents; } NewBalanceDataGridView.Columns[i].Resizable = DataGridViewTriState.True; } NewBalanceDataGridView.SelectionChanged += NewBalanceDataGridView_SelectionChanged; }
// Logic Value Changed Event Handler void NewBalance_CellValueChanged(object sender, PropertyChangedEventArgs e) { Cell c = sender as Cell; if (e.PropertyName == "String") { if (sender is KCell) { KCell k = sender as KCell; if (k.KValue != String.Empty) { NewBalanceDataGridView.Rows[k.RowIndex].Cells[k.ColumnIndex].Value = k.KValue; } else { NewBalanceDataGridView.Rows[k.RowIndex].Cells[k.ColumnIndex].Value = k.Value.ToString(); } } else if (sender is MCell) { MCell m = sender as MCell; NewBalanceDataGridView.Rows[m.RowIndex].Cells[m.ColumnIndex].Value = m.MValue; } } else { if (c != null && c.Value == null) { if (c is KCell) { KCell kc = c as KCell; if (kc.KValue != String.Empty) { NewBalanceDataGridView.Rows[c.RowIndex].Cells[c.ColumnIndex].Value = kc.KValue; } else { NewBalanceDataGridView.Rows[c.RowIndex].Cells[c.ColumnIndex].Value = null; } } else if (c is MultipleCell) { MultipleCell mc = c as MultipleCell; if (mc.SelectedValue != null) { NewBalanceDataGridView.Rows[c.RowIndex].Cells[c.ColumnIndex].Value = mc.SelectedValue; } else { NewBalanceDataGridView.Rows[c.RowIndex].Cells[c.ColumnIndex].Value = null; } } else { NewBalanceDataGridView.Rows[c.RowIndex].Cells[c.ColumnIndex].Value = null; } } else { if (c != null) { // need to check if there is a calculation with sender as a dependency ViewData.CheckAndUpdateDependency(c); NewBalanceDataGridView.Rows[c.RowIndex].Cells[c.ColumnIndex].Value = c.Value.ToString(); if (ViewData.HasMultipleCells()) { MultipleCell mc = null; for (int i = 0; i < NewBalanceDataGridView.ColumnCount; i++) { if (ViewData.GetCellType(c.RowIndex, i) == CellType.Multiple) { mc = ViewData.GetCell(c.RowIndex, i) as MultipleCell; break; } } if (mc == null) { return; } // Update Value to the currently selected cell ViewData.CheckAndUpdateMultipleDependency(c, mc); if (mc.ColumnIndex == c.ColumnIndex && mc.RowIndex == c.RowIndex) { NewBalanceDataGridView.Rows[c.RowIndex].Cells[c.ColumnIndex].Value = mc.SelectedValue.ToString(); } } } } } }