public static ElectronicTable FillFromText(string data) { try { var reader = new StringReader(data); var sizes = reader.ReadLine(); var n = int.Parse(sizes.Substring(0, sizes.IndexOf(' '))); var m = int.Parse(sizes.Substring(sizes.IndexOf(' ') + 1)); Console.WriteLine($"created table with sizes {n} and {m}"); ElectronicTable table = new ElectronicTable(n, m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table.Cell(i, j).Expression = reader.ReadLine(); Console.WriteLine($"in cell ({i},{j}) now {table.Cell(i, j).Expression}"); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table.CalculateAllCells(table.Cell(i, j)); Console.WriteLine($"value of ({i},{j}) now {table.Cell(i, j).Value}"); } } return(table); } catch { throw; } }
void AddTable(ElectronicTable table) { if (table == null) { Table = new ElectronicTable(20, 10) { Location = new Point(40, MainPanel.Bottom + 20), Size = new Size(ClientSize.Width - 80, ClientSize.Height - MainPanel.Bottom - 80), }; } else { Table.SuspendLayout(); Controls.Remove(Table); table.Location = new Point(30, MainPanel.Bottom + 30); table.Size = new Size(ClientSize.Width - 60, ClientSize.Height - MainPanel.Bottom - 60); Table = table; } Table.CellEnter += (s, e) => { ElectronicTableCell selected = Table.Cell(e.RowIndex, e.ColumnIndex) as ElectronicTableCell; ExpressionTextBox.Text = Table.Cell(e.RowIndex, e.ColumnIndex).Expression; }; Table.CellBeginEdit += (s, e) => { try { Table.CurCell.Value = ExpressionTextBox.Text = Table.Cell(e.RowIndex, e.ColumnIndex).Expression; } catch (IndexOutOfRangeException) { } }; Table.CellEndEdit += (s, e) => { ElectronicTableCell changed = Table.Cell(e.RowIndex, e.ColumnIndex); string oldExpr = changed.Expression; changed.Expression = (string)changed.Value; try { Table.CalculateAllCells(changed); } catch (Exception ex) { if (ex.Data.Contains("Type")) { MessageBox.Show($"Wrong expression: {ex.Data["Type"]}", "Error"); } else { MessageBox.Show($"Wrong expression", "Error"); } changed.Expression = oldExpr; Table.CalculateAllCells(changed); } TableChanged = true; Text = "*Table"; }; Controls.Add(Table); Table.ResumeLayout(); }
void AddFileButtons() { OpenAboutButton = new Button() { Location = new Point( RemoveColumn.Right + Math.Max(20, (ClientRectangle.Right - 900 - ExpressionTextBox.Right) / 2 - 30) , 10), Size = new Size(80, 30), Text = "About", Font = new Font("Arial", 14), }; OpenAboutButton.Click += (s, e) => { MessageBox.Show("Сивокобильска Іра, К-25, варіант №5\nІндивідуальне завдання: *, /, +, -, div, mod, ^, mmax(), mmin()", "About", MessageBoxButtons.OK); }; SaveToButton = new Button() { Location = new Point(OpenAboutButton.Right + 15, OpenAboutButton.Top), Size = new Size(80, 30), Text = "Save", Font = new Font("Arial", 14), }; SaveToButton.Click += (s, e) => { var saveTo = new SaveFileDialog() { Filter = "Text files (*.txt)|*.txt", }; if (saveTo.ShowDialog() == DialogResult.OK) { File.WriteAllText(saveTo.FileName, Table.ConvertToText()); } TableChanged = false; Text = "Table"; }; Open = new Button() { Location = new Point(SaveToButton.Right + 15, OpenAboutButton.Top), Size = new Size(80, 30), Text = "Open", Font = new Font("Arial", 14), }; Open.Click += (s, e) => { if (TableChanged) { var isSave = MessageBox.Show("Do you wanna save this table before you open other?", "Saving", MessageBoxButtons.YesNoCancel); if (isSave == DialogResult.Yes) { SaveToButton.PerformClick(); } } var openFrom = new OpenFileDialog() { Filter = "Text files (*.txt)|*.txt", }; if (openFrom.ShowDialog() == DialogResult.OK) { try { AddTable( ElectronicTable.FillFromText( File.ReadAllText(openFrom.FileName))); } catch { MessageBox.Show($"Wrong file: {openFrom.FileName}", "Wrong file"); } } }; Clear = new Button() { Location = new Point(Open.Right + 15, OpenAboutButton.Top), Size = new Size(70, 30), Text = "Clear", Font = new Font("Arial", 14), }; Clear.Click += (s, e) => { Table.Clear(); }; MainPanel.Controls.Add(OpenAboutButton); MainPanel.Controls.Add(SaveToButton); MainPanel.Controls.Add(Open); MainPanel.Controls.Add(Clear); }