//Apps Menu Item Clicked Event Handler void item_Click(object sender, EventArgs e) { ToolStripMenuItem t = sender as ToolStripMenuItem; if (t == null) { return; } if (NewBalanceDataGridView.RowCount > 0) { SaveCheck sc = new SaveCheck(); sc.LabelCorrectlyFromLoad(); DialogResult result = sc.ShowDialog(); if (result == DialogResult.Yes && !string.IsNullOrEmpty(_currentOpenFile)) { saveToolStripMenuItem_Click(this, EventArgs.Empty); } else if (result == DialogResult.Retry || string.IsNullOrEmpty(_currentOpenFile)) { saveAsToolStripMenuItem_Click(this, EventArgs.Empty); } } _currentOpenFile = ""; //find item in app string nameOfApp = t.Text; Text = "NewBalance - " + t.Text; try { EmptyGridView(nameOfApp); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK); } addRowButton.Enabled = true; // don't show save here unless they have saved the file already saveAsToolStripMenuItem.Enabled = true; }
//Load File Event Handler private void loadToolStripMenuItem_Click(object sender, EventArgs e) { // add string to keep current file name after openfiledialog OpenFileDialog o = new OpenFileDialog() { InitialDirectory = Settings.Default.DefaultPath, RestoreDirectory = true }; if (NewBalanceDataGridView.RowCount > 0) { SaveCheck sc = new SaveCheck(); sc.LabelCorrectlyFromLoad(); DialogResult result = sc.ShowDialog(); if (result == DialogResult.Yes && !string.IsNullOrEmpty(_currentOpenFile)) { saveToolStripMenuItem_Click(this, EventArgs.Empty); } else if (result == DialogResult.Retry || string.IsNullOrEmpty(_currentOpenFile)) { saveAsToolStripMenuItem_Click(this, EventArgs.Empty); } } if (o.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(o.FileName, FileMode.Open); StreamReader r = new StreamReader(fs); // first line is the app name string appName = r.ReadLine(); try { EmptyGridView(appName); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK); return; } this.Text = "NewBalance - " + appName; string s; int row = 0, column; double placeHolder; bool firstLine = true; while ((s = r.ReadLine()) != null) { if (!firstLine) AddRowToDataAndView(); var items = s.Split(','); column = 0; foreach (string item in items) { if (!String.IsNullOrWhiteSpace(item)) { if (double.TryParse(item, out placeHolder)) ViewData.GetCell(row, column).Value = placeHolder; // string, need to set correct string value for celltype // only K has a string value else { KCell kc = ViewData.GetCell(row, column) as KCell; if (kc != null) { kc.KValue = item; } } } column++; } firstLine = false; row++; } r.Close(); fs.Close(); saveToolStripMenuItem.Enabled = true; saveAsToolStripMenuItem.Enabled = true; addRowButton.Enabled = true; // Don't annoy user if they just loaded the file RecentlySaved = true; } }
//Apps Menu Item Clicked Event Handler void item_Click(object sender, EventArgs e) { ToolStripMenuItem t = sender as ToolStripMenuItem; if (t == null) return; if (NewBalanceDataGridView.RowCount > 0) { SaveCheck sc = new SaveCheck(); sc.LabelCorrectlyFromLoad(); DialogResult result = sc.ShowDialog(); if (result == DialogResult.Yes && !string.IsNullOrEmpty(_currentOpenFile)) { saveToolStripMenuItem_Click(this, EventArgs.Empty); } else if(result == DialogResult.Retry || string.IsNullOrEmpty(_currentOpenFile)) { saveAsToolStripMenuItem_Click(this, EventArgs.Empty); } } _currentOpenFile = ""; //find item in app string nameOfApp = t.Text; Text = "NewBalance - " + t.Text; try { EmptyGridView(nameOfApp); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK); } addRowButton.Enabled = true; // don't show save here unless they have saved the file already saveAsToolStripMenuItem.Enabled = true; }
//Load File Event Handler private void loadToolStripMenuItem_Click(object sender, EventArgs e) { // add string to keep current file name after openfiledialog OpenFileDialog o = new OpenFileDialog() { InitialDirectory = Settings.Default.DefaultPath, RestoreDirectory = true }; if (NewBalanceDataGridView.RowCount > 0) { SaveCheck sc = new SaveCheck(); sc.LabelCorrectlyFromLoad(); DialogResult result = sc.ShowDialog(); if (result == DialogResult.Yes && !string.IsNullOrEmpty(_currentOpenFile)) { saveToolStripMenuItem_Click(this, EventArgs.Empty); } else if (result == DialogResult.Retry || string.IsNullOrEmpty(_currentOpenFile)) { saveAsToolStripMenuItem_Click(this, EventArgs.Empty); } } if (o.ShowDialog() == DialogResult.OK) { FileStream fs = new FileStream(o.FileName, FileMode.Open); StreamReader r = new StreamReader(fs); // first line is the app name string appName = r.ReadLine(); try { EmptyGridView(appName); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK); return; } this.Text = "NewBalance - " + appName; string s; int row = 0, column; double placeHolder; bool firstLine = true; while ((s = r.ReadLine()) != null) { if (!firstLine) { AddRowToDataAndView(); } var items = s.Split(','); column = 0; foreach (string item in items) { if (!String.IsNullOrWhiteSpace(item)) { if (double.TryParse(item, out placeHolder)) { ViewData.GetCell(row, column).Value = placeHolder; } // string, need to set correct string value for celltype // only K has a string value else { KCell kc = ViewData.GetCell(row, column) as KCell; if (kc != null) { kc.KValue = item; } } } column++; } firstLine = false; row++; } r.Close(); fs.Close(); saveToolStripMenuItem.Enabled = true; saveAsToolStripMenuItem.Enabled = true; addRowButton.Enabled = true; // Don't annoy user if they just loaded the file RecentlySaved = true; } }