private void loadToolStripMenuItem_Click(object sender, EventArgs e) { //options for filetypes in the open file window openFileDialog1.Filter = "All files (*.*)|*.*|xml files (*.xml)|*.xml"; openFileDialog1.FilterIndex = 2; //checks if the user selected a file if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // loading from the xml file using (XMLInterface loadFile = new XMLInterface(openFileDialog1.OpenFile())) if (loadFile != null) //if the path isnt null { try { sheet = loadFile.XMLLoad(); sheet.CellPropertyChanged += Sheet_CellPropertyChanged; //subscribes to the sheets event handler selectedCell = sheet.GetCell(0, 49) as SpreadsheetCell; //sets a default selected cell to prevent crashes ForceSheetUpdate(); } catch (Exception except) { Console.WriteLine(except.Message); } } } }
private void SpreadsheetChangedEvent(object sender, PropertyChangedEventArgs e) { SpreadsheetCell cell = (SpreadsheetCell)sender; this.dataGridView1[cell.ColumnIndex, cell.RowIndex].Value = this.engine.Sheet[cell.RowIndex, cell.ColumnIndex].Value; this.dataGridView1[cell.ColumnIndex, cell.RowIndex].Style.BackColor = Color.FromArgb((int)(this.engine.Sheet[cell.RowIndex, cell.ColumnIndex].BGColor)); }
private void FillDefaultData(ExtendedDataGrid exGrid) { DataTable dataTable = new DataTable("DataTable" + this.NumberOfSheets); int rowsNum = 17; int colsNum = 13; for (int i = 0; i < colsNum; i++) { dataTable.Columns.Add(new DataColumn(SSColumns.ToString(i), typeof(SpreadsheetCell))); } for (int i = 0; i < rowsNum; i++) { var row = dataTable.NewRow(); dataTable.Rows.Add(row); for (int j = 0; j < colsNum; j++) { string cellName = SSColumns.ToString(j) + i; row[j] = new SpreadsheetCell(string.Empty, dataTable, new SpreadsheetCellCustomInfo(new CellName(cellName, i, j))); } } this.DataTables.Add(dataTable); exGrid.ItemsSource = dataTable.DefaultView; }
/// <summary> /// When a dataGridView1 cell begins to be edited, set its value to equal /// its corresponding spreadsheet cell's text value. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event arguments.</param> private void DataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { DataGridViewCell dgCell = this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; SpreadsheetCell ssCell = this.spreadsheet.GetCell(e.RowIndex, e.ColumnIndex); dgCell.Value = ssCell.Text; }
private void changeBackgroundColorToolStripMenuItem_Click(object sender, EventArgs e) { ColorDialog MyDialog = new ColorDialog(); // Keeps the user from selecting a custom color. MyDialog.AllowFullOpen = true; // Allows the user to get help. (The default is false.) MyDialog.ShowHelp = true; // Update the text box color if the user clicks OK. if (MyDialog.ShowDialog() == DialogResult.OK) { ColorChange changes = new ColorChange(); foreach (DataGridViewCell cell in this.dataGridView1.SelectedCells) { SpreadsheetCell engineCell = this.engine.Sheet[cell.RowIndex, cell.ColumnIndex]; changes.AddCell(ref engineCell, engineCell.BGColor, (uint)MyDialog.Color.ToArgb()); engineCell.BGColor = (uint)MyDialog.Color.ToArgb(); } this.engine.UndoStack.Push(new ColorChangeCommand(changes)); this.engine.UndoEmpty = false; this.undoToolStripMenuItem.Enabled = true; } }
// Event handler to update UI's value in appropriate location public void SheetUpdate(object sender, PropertyChangedEventArgs e) { SpreadsheetCell updatedCell = (SpreadsheetCell)sender; string updatedValue = e.PropertyName; dataGridView1.Rows[updatedCell.getRowIndex()].Cells[updatedCell.getColumnIndex()].Value = updatedValue; }
public void TestSpreadsheetGetCellNormal() { Spreadsheet s = new Spreadsheet(5, 5); SpreadsheetCell newCell = new SpreadsheetCell(0, 0); Assert.That(s.GetCell(4, 0).Value, Is.EqualTo(newCell.Value), "Spreadsheet returned undexpected cell at index."); }
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { SpreadsheetCell toEdit = mainSS[e.RowIndex, e.ColumnIndex]; if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != null) { string currentVal = toEdit.Value; // Stores the current value for later comparison string prev = toEdit.Text; // Stores the previous text string fromGrid = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(); mainSS[e.RowIndex, e.ColumnIndex].Text = fromGrid; // Updates the value of the current cell if (dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() == prev) // If no change occured { dataGridView1[e.ColumnIndex, e.RowIndex].Value = currentVal; // replaces the current cell with its old value return; } mainSS.PushUndo(new CmdCollection(new RestoreText(mainSS[e.RowIndex, e.ColumnIndex], prev))); redoToolStripMenuItem.Enabled = false; // The redo stack is cleared after a new property is changed, so redo button should be disabled redoToolStripMenuItem.Text = "Redo"; } else { toEdit.Text = ""; } if (mainSS.UndoCount > 0) { undoToolStripMenuItem.Text = "Undo - " + mainSS.PeekUndo; // Tells the UI which property will be restored } }
private void Form1_Load(object sender, EventArgs e) { sheet = new Spreadsheet(26, 50); //makes a 50 by 26 spreadsheet sheet.CellPropertyChanged += Sheet_CellPropertyChanged; //subscribes to the sheets event handler selectedCell = sheet.GetCell(0, 0) as SpreadsheetCell; //sets a default selected cell to prevent crashes MakeCells(); }
/// <summary> /// Click button demo handle. /// </summary> /// <param name="sender">sender.</param> /// <param name="e">e.</param> private void DemoClick(object sender, EventArgs e) { Random r1 = new Random(); Random r2 = new Random(); // write word in random 50 cells. for (int i = 0; i < 50; i++) { int row = r1.Next(50); int colum = r2.Next(26); SpreadsheetCell index = this.table.GetCell(row, colum); index.Text = "Hello World"; } // write word in all B cell. for (int i = 0; i < this.table.RowCount; i++) { SpreadsheetCell index = this.table.GetCell(i, 1); index.Text = "This is cell B" + Convert.ToString(i + 1); } // use to test = way to copy cell. SpreadsheetCell index2 = this.table.GetCell(0, 0); index2.Text = "=B" + Convert.ToString(1); }
public void TestColor() { Spreadsheet testSheet = new Spreadsheet(50, 26); SpreadsheetCell cell1 = testSheet.GetCell(0, 0) as SpreadsheetCell; // FF6600 is a sort of orange from some random color picker I found online. should match. cell1.BGColor = 0xFF6600; Assert.AreEqual(cell1.BGColor, 0xFF6600); }
/// <summary> /// Begin to edit handle. /// </summary> /// <param name="sender">sender.</param> /// <param name="e">e.</param> private void DataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { string msg = string.Format("Editing Cell at ({0}, {1})", e.ColumnIndex, e.RowIndex); this.Text = msg; SpreadsheetCell index = this.table.GetCell(e.RowIndex, e.ColumnIndex); this.dataGridView1.CurrentCell.Value = index.Text; }
private void color_changing(object sender, bool custom) { ColorDialog SelectColor = new ColorDialog(); if (custom) // user wants to select their own color -> prompts color dialogue { if (SelectColor.ShowDialog() != DialogResult.OK) { return; } } else // the user chose one of the pre-selected colors { if (sender.ToString() == "Reset") { SelectColor.Color = System.Drawing.Color.FromArgb(-1); } if (sender.ToString() == "Green") { SelectColor.Color = System.Drawing.Color.FromArgb(192, 255, 192); } else if (sender.ToString() == "Red") { SelectColor.Color = System.Drawing.Color.FromArgb(255, 128, 128); } else if (sender.ToString() == "Orange") { SelectColor.Color = System.Drawing.Color.FromArgb(255, 192, 128); } else if (sender.ToString() == "Grey") { SelectColor.Color = System.Drawing.Color.FromArgb(131, 130, 120); } } DataGridViewSelectedCellCollection CellsChanging = dataGridView1.SelectedCells; // Gets the collection of selected cells CmdCollection BGRestore = new CmdCollection(); if (CellsChanging != null) // Ensures that there is at least one selected cell { foreach (DataGridViewCell cell in CellsChanging) // Foreach cell that has been selected, notify the spreadsheet of that cells property change { SpreadsheetCell Changing = mainSS[cell.RowIndex, cell.ColumnIndex]; RestoreBGColor newRestore = new RestoreBGColor(Changing, Changing.BGColor); Changing.BGColor = SelectColor.Color.ToArgb(); // Converts the chosen color to ARBG integer format BGRestore.Add(newRestore); } mainSS.PushUndo(BGRestore); // Adds the new restore collection undoToolStripMenuItem.Text = "Undo - " + mainSS.PeekUndo; // Tells the UI which property will be undone redoToolStripMenuItem.Enabled = false; redoToolStripMenuItem.Text = "Redo"; } }
private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { SpreadsheetCell cell = this.engine.Sheet[e.RowIndex, e.ColumnIndex]; TextChange newchange = new TextChange(ref cell, cell.Text, this.dataGridView1[e.ColumnIndex, e.RowIndex].Value?.ToString()); this.engine.UndoStack.Push(new TextChangeCommand(newchange)); this.engine.UndoEmpty = false; this.undoToolStripMenuItem.Enabled = true; this.engine.Sheet[e.RowIndex, e.ColumnIndex].Text = this.dataGridView1[e.ColumnIndex, e.RowIndex].Value?.ToString() ?? string.Empty; this.dataGridView1[e.ColumnIndex, e.RowIndex].Value = this.engine.Sheet[e.RowIndex, e.ColumnIndex].Value; }
public static SpreadsheetCell[,] String2DArrayToSpreadsheetCell2DArray(string[,] array) { var result = new SpreadsheetCell[array.GetLength(0), array.GetLength(1)]; for (int x = 0; x < array.GetLength(0); x++) { for (int y = 0; y < array.GetLength(1); y++) { result[x, y] = new SpreadsheetCell(array[x, y]); } } return(result); }
private void OnCellPropertyChanged(object sender, PropertyChangedEventArgs e) { SpreadsheetCell cell = sender as SpreadsheetCell; if (e.PropertyName == "Value" || e.PropertyName == "Text") { dataGridView1.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Value = cell.Value; } if (e.PropertyName == "bgColor") { dataGridView1.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Style.BackColor = ColorTranslator.FromHtml(cell.bgColor); } }
private void OnCellClick(object sender, DataGridViewCellEventArgs e) { int rowInd = e.RowIndex; int colInd = e.ColumnIndex; SpreadsheetCell cell = spc.GetCell(rowInd, colInd) as SpreadsheetCell; // gets cell from SpreadSheetClass instance editCell = cell; // ensure text is not null / empty if (cell.Text != null) //|| cell.Text != "" { textBox1.Text = cell.Text; // getting the value at the specified cell and setting it to the appropriate text } }
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { selectedCell.Text = textBox1.Text; //save the text currently being typed try { selectedCell = sheet.GetCell(e.ColumnIndex, e.RowIndex) as SpreadsheetCell; //saves a reference to the current cell textBox1.Text = selectedCell.Text; //updates the textbox with the text of the cell } catch (IndexOutOfRangeException exception) //if the cell clicked is something outside of the spreadsheet { Console.WriteLine(exception.Message); } }
/// <summary> /// Initializes each DG cell's properties to equal its spreadsheet cell counterpart's properties. /// </summary> private void InitializeDGCells() { for (int i = 0; i < this.DataGridView1.Rows.Count; i++) { for (int j = 0; j < this.DataGridView1.Columns.Count; j++) { DataGridViewCell dgCell = this.DataGridView1.Rows[i].Cells[j]; SpreadsheetCell ssCell = this.spreadsheet.GetCell(i, j); dgCell.Style.BackColor = Color.FromArgb((int)ssCell.BGColor); dgCell.Value = ssCell.Value; } } }
/// <summary> /// When a dataGridView1 cell ends being edited, /// set the ss cell's text value to equal the cc cell's value, /// and reset the dg cell to the ss cell's value. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event arguments.</param> private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { DataGridViewCell dgCell = this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; SpreadsheetCell ssCell = this.spreadsheet.GetCell(e.RowIndex, e.ColumnIndex); string newText = dgCell.Value.ToString(); CellTextCommand cmd = new CellTextCommand(ssCell, newText); this.spreadsheet.AddUndo(cmd); this.UpdateUndoRedoButtons(); dgCell.Value = ssCell.Value; }
/// <summary> /// Cell Property Changed Spreadsheet event. /// If the value of the SpreadsheetCell changed, updates dataGridView1 cell value. /// If the bgColor of the SpreadsheetCell changed, updates its background color. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event Arguments.</param> private void UpdateCellProperty(object sender, PropertyChangedEventArgs e) { SpreadsheetCell ssCell = sender as SpreadsheetCell; DataGridViewCell dgCell = this.DataGridView1.Rows[ssCell.RowIndex].Cells[ssCell.ColumnIndex]; if (e.PropertyName.Equals("Value")) { dgCell.Value = ssCell.Value; } else if (e.PropertyName.Equals("BGColor")) { Color newColor = Color.FromArgb((int)ssCell.BGColor); dgCell.Style.BackColor = newColor; } }
public static SpreadsheetCell[][] SpreadsheetCell2DArrayToJaggedArray(SpreadsheetCell[,] spreadsheet) { var convertedSpreadsheet = new SpreadsheetCell[spreadsheet.GetLength(0)][]; for (var indexX = 0; indexX < spreadsheet.GetLength(0); indexX++) { convertedSpreadsheet[indexX] = new SpreadsheetCell[spreadsheet.GetLength(1)]; for (var indexY = 0; indexY < spreadsheet.GetLength(1); indexY++) { convertedSpreadsheet[indexX][indexY] = spreadsheet[indexX, indexY]; } } return(convertedSpreadsheet); }
// Initialize cells public Spreadsheet(int cols, int rows) { cellArray = new SpreadsheetCell[cols, rows]; for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { cellArray[i, j] = new SpreadsheetCell(i, j); cellArray[i, j].PropertyChanged += OnCellPropertyChanged; } } refDict = new Dictionary <string, HashSet <SpreadsheetCell> >(); Undos = new Stack <UndoRedoCollection>(); Redos = new Stack <UndoRedoCollection>(); }
/// <summary> /// Resize table by creating new and copying old values /// </summary> /// <param name="exGrid"></param> /// <param name="oldDataTable"></param> /// <param name="rowsNum"></param> /// <param name="colsNum"></param> private void ResizeTable(ExtendedDataGrid exGrid, DataTable oldDataTable, int rowsNum, int colsNum) { int boundRows = oldDataTable.Rows.Count < rowsNum ? oldDataTable.Rows.Count : rowsNum; int boundCols = oldDataTable.Columns.Count < colsNum ? oldDataTable.Columns.Count : colsNum; DataTable newDataTable = new DataTable(oldDataTable.TableName); for (int i = 0; i < colsNum; i++) { newDataTable.Columns.Add(new DataColumn(SSColumns.ToString(i), typeof(SpreadsheetCell))); } for (int i = 0; i < rowsNum; i++) { var row = newDataTable.NewRow(); newDataTable.Rows.Add(row); for (int j = 0; j < colsNum; j++) { string cellName = SSColumns.ToString(j) + i; row[j] = new SpreadsheetCell(string.Empty, newDataTable, new SpreadsheetCellCustomInfo(new CellName(cellName, i, j))); } } // copy data for (int i = 0; i < boundRows; i++) { for (int j = 0; j < boundCols; j++) { SpreadsheetCell cell = newDataTable.GetSpreadsheetCell(i, j); SpreadsheetCell oldCell = oldDataTable.GetSpreadsheetCell(i, j); cell.Content = oldCell.Content; cell.Tag = oldCell.Tag; } } this.DataTables[this.CurrentSheetNumber] = newDataTable; // VERY VERY DIRTY HACK because nulling throws exception, dunno why try { exGrid.ItemsSource = null; } catch { } exGrid.ItemsSource = newDataTable.DefaultView; }
// This function checks circular references private bool CircularRef(List <string> varNames, SpreadsheetCell cell) { // For every variable in that is referenced in the new cell edit... foreach (var name in varNames) { // Create a new stack with the referenced cell in it Stack <SpreadsheetCell> stack = new Stack <SpreadsheetCell>(); stack.Push((GetCell(Convert.ToInt32(name[0]) - 65, Convert.ToInt32(name.Substring(1)) - 1) as SpreadsheetCell)); // Call circular helper if (CircularHelper(cell, stack)) { return(true); } } return(false); }
// referred to the page below for linking changes in the DataGridView back to the SpreadsheetEngine // https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellendedit(v=vs.110).aspx private void dgv1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { int rowInd = e.RowIndex; int colInd = e.ColumnIndex; SpreadsheetCell cell = spc.GetCell(rowInd, colInd) as SpreadsheetCell; // gets cell from SpreadSheetClass instance DataGridView dgv = sender as DataGridView; if (cell.Value == dgv[colInd, rowInd].ToString()) //forget identical values { return; } if (dgv[colInd, rowInd].Value != null) //if text in DataGridView cell, update text to trigger events { cell.Text = dgv[colInd, rowInd].Value.ToString(); textBox1.Text = cell.Text; } //dgv[colInd, rowInd].Value = cell.Value; //would defeat the purpose of events/handlers }
/// <summary> /// Updates the background color of currently selected cells in the spreadsheet. /// Users select a color with a color dialog window. /// Updates the undo/redo menu buttons. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event arguments.</param> private void ChangeBackgroundColorToolStripMenuItem_Click(object sender, EventArgs e) { if (this.colorDialog.ShowDialog() == DialogResult.OK) { uint newColor = (uint)this.colorDialog.Color.ToArgb(); List <SpreadsheetCell> changingCells = new List <SpreadsheetCell>(); foreach (DataGridViewCell dgCell in this.DataGridView1.SelectedCells) { SpreadsheetCell ssCell = this.spreadsheet.GetCell(dgCell.RowIndex, dgCell.ColumnIndex); changingCells.Add(ssCell); } BGColorCommand cmd = new BGColorCommand(changingCells, newColor); this.spreadsheet.AddUndo(cmd); this.UpdateUndoRedoButtons(); } }
/// <summary> /// Loads data tables from file *.ispf using binary deserializer /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OpenFileButton_OnClick(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.DefaultExt = ".ispf"; dlg.Filter = "iSpreadsheet document format (.ispf)|*.ispf"; if (dlg.ShowDialog() == true) { try { string filename = dlg.FileName; BinaryFormatter formatter = new BinaryFormatter(); using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { this.DataTables = (List <DataTable>)formatter.Deserialize(stream); this.WorkspaceTabControl.Items.Clear(); this.NumberOfSheets = 0; // Update cell parent table referencing, cause it's not serializable foreach (var dataTable in this.DataTables) { for (int i = 0; i < dataTable.Rows.Count; i++) { for (int j = 0; j < dataTable.Columns.Count; j++) { SpreadsheetCell cell = dataTable.GetSpreadsheetCell(i, j); cell.Table = dataTable; } } this.GenerateSheet(dataTable); } } Logger.WriteLogInfo("File \"" + filename + "\" was loaded successfully! Please wait for UI loading!"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error occured during saving file", MessageBoxButton.OK, MessageBoxImage.Error); Logger.WriteLogException(ex.GetType().Name + ": " + ex.Message); } } }
public static string[] SaveVectorDistanceMatrixSpreadsheet(string saveFilename, List <VectorProteinInterfaceWhole> vectorProteinInterfaceWholeList, double[,] vectorDistanceMatrix) { if (saveFilename == null) { throw new ArgumentNullException(nameof(saveFilename)); } if (vectorProteinInterfaceWholeList == null) { throw new ArgumentNullException(nameof(vectorProteinInterfaceWholeList)); } if (vectorDistanceMatrix == null) { throw new ArgumentNullException(nameof(vectorDistanceMatrix)); } var spreadsheet = new SpreadsheetCell[vectorDistanceMatrix.GetLength(0) + 1, vectorDistanceMatrix.GetLength(1) + 1]; for (var indexX = 0; indexX < vectorDistanceMatrix.GetLength(0); indexX++) { spreadsheet[0, indexX + 1] = new SpreadsheetCell(vectorProteinInterfaceWholeList[indexX].FullProteinInterfaceId.ToString()); } spreadsheet[0, 0] = new SpreadsheetCell(""); for (var indexY = 0; indexY < vectorDistanceMatrix.GetLength(1); indexY++) { spreadsheet[indexY + 1, 0] = new SpreadsheetCell(vectorProteinInterfaceWholeList[indexY].FullProteinInterfaceId.ToString()); } for (var indexX = 0; indexX < vectorDistanceMatrix.GetLength(0); indexX++) { for (var indexY = 0; indexY < vectorDistanceMatrix.GetLength(1); indexY++) { spreadsheet[indexX + 1, indexY + 1] = new SpreadsheetCell(vectorDistanceMatrix[indexX, indexY]); } } var savedFiles = SpreadsheetFileHandler.SaveSpreadsheet(saveFilename, null, spreadsheet); return(savedFiles); }
/// <summary> /// End to edit handle. /// </summary> /// <param name="sender">sender.</param> /// <param name="e">e.</param> private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { string msg = string.Format("Finished Editing Cell at ({0}, {1})", e.ColumnIndex, e.RowIndex); this.Text = msg; SpreadsheetCell index = this.table.GetCell(e.RowIndex, e.ColumnIndex); if (this.dataGridView1.CurrentCell.Value != null) { string prevText = index.Text; index.Text = this.dataGridView1.CurrentCell.Value.ToString(); UndoRedoCollection UndoCommand = new UndoRedoCollection("Text Changes"); UndoCommand.SetTextVariable(index, prevText, index.Text); this.table.AddUndo(UndoCommand); this.undoToolStripMenuItem.Text = this.table.UndoText; } else { this.dataGridView1.CurrentCell.Value = string.Empty; } }