//----------------------------------------------------------------------------- #region ** ctor public MainWindow() { InitializeComponent(); // set language to match the system settings this.Language = System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.Name); // connect function menu to _tbFx TextBlock var fnMenu = new ExcelCalcFunctionMenu(); #if false fnMenu.ItemClick += (s, e) => { var item = e.Source as C1MenuItem; var text = (string)item.Header; var selStart = _txtFormula.SelectionStart; _txtFormula.SelectedText = text + "()"; _txtFormula.SelectionStart = selStart + text.Length + 1; _txtFormula.Focus(); }; #endif _tbFx.MouseLeftButtonDown += (s, e) => { fnMenu.IsOpen = true; //fnMenu.Show(_tbFx, new Point(_tbFx.RenderSize.Width - 4, _tbFx.RenderSize.Height - 4)); }; // add some sheets _flex.AddSheet("Sheet1", 50, 10); _flex.AddSheet("Sheet2", 50, 10); _flex.AddSheet("Sheet3", 50, 10); _flex.Sheets.SelectedIndex = 0; // populate the grid with some formulas (multiplication table) for (int r = 0; r < _flex.Rows.Count - 2; r++) { for (int c = 0; c < _flex.Columns.Count; c++) { _flex[r, c] = string.Format("={0}*{1}", r + 1, c + 1); } } // add a totals row to illustrate var totRow = _flex.Rows.Count - 1; for (int c = 0; c < _flex.Columns.Count; c++) { _flex[totRow, c] = string.Format("=sum({0}1:{0}{1})", (char)('A' + c), _flex.Rows.Count - 3); } _flex.Rows[totRow].FontWeight = FontWeights.Bold; // set up event handler to update the selection status _flex.SelectionChanged += (s, e) => { var sel = _flex.Selection; // update address label var text = string.Empty; if (sel.IsValid) { text = _flex.GetAddress(sel, false); } _tbAddress.Text = text; // update formula textbox text = string.Empty; if (sel.IsValid) { var row = _flex.Rows[sel.Row] as ExcelRow; if (row != null) { var col = _flex.Columns[sel.Column]; text = row.GetDataEditor(col); } } _txtFormula.Text = text; _txtFormula.Tag = text; // update status bar text = "Ready"; if (sel.IsValid && !sel.IsSingleCell && sel.BottomRow < _flex.Rows.Count && sel.RightColumn < _flex.Columns.Count) { try { var address = _flex.GetAddress(sel, true); var avg = _flex.Evaluate(string.Format("Average({0})", address)); var cnt = _flex.Evaluate(string.Format("Count({0})", address)); var sum = _flex.Evaluate(string.Format("Sum({0})", address)); if ((double)cnt > 0) { text = string.Format("Average: {0:#,##0.##} Count: {1:n0} Sum: {2:#,##0.##}", avg, cnt, sum); } } catch { // circular reference/bad expression? } } _tbStatus.Text = text; }; // update grid when user types into formula bar _txtFormula.LostFocus += (s, e) => { var sel = _flex.Selection; if (sel.IsValid) { _txtFormula.Tag = _txtFormula.Text; _flex[sel.Row, sel.Column] = _txtFormula.Text; } }; _txtFormula.KeyDown += (s, e) => { switch (e.Key) { // apply formula when user hits enter case Key.Enter: _flex.Focus(); break; // restore original value when user hits escape case Key.Escape: _txtFormula.Text = _txtFormula.Tag as string; _txtFormula.SelectAll(); break; } }; // give grid the focus when the app loads Loaded += (s, e) => { _flex.Focus(); }; #if false // demonstrate Excel notation var val = _flex["B4"]; _flex["B4"] = "Orange"; System.Diagnostics.Debug.WriteLine("cell 'b4' was '{0}' and is now '{1}'", val, _flex["b4"]); #endif // create custom menu items CustomizeContextMenu(); // start with blue color scheme SetColorScheme(ColorScheme.Blue); }
public MainWindow() { InitializeComponent(); // set language to match the system settings this.Language = System.Windows.Markup.XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.Name); // connect function menu to _tbFx TextBlock var fnMenu = new ExcelCalcFunctionMenu(); #if false fnMenu.ItemClick += (s, e) => { var item = e.Source as C1MenuItem; var text = (string)item.Header; var selStart = _txtFormula.SelectionStart; _txtFormula.SelectedText = text + "()"; _txtFormula.SelectionStart = selStart + text.Length + 1; _txtFormula.Focus(); }; #endif _tbFx.MouseLeftButtonDown += (s, e) => { fnMenu.IsOpen = true; //fnMenu.Show(_tbFx, new Point(_tbFx.RenderSize.Width - 4, _tbFx.RenderSize.Height - 4)); }; // add some sheets _flex.AddSheet("Sheet1", 50, 10); _flex.AddSheet("Sheet2", 50, 10); _flex.AddSheet("Sheet3", 50, 10); _flex.Sheets.SelectedIndex = 0; // populate the grid with some formulas (multiplication table) for (int r = 0; r < _flex.Rows.Count - 2; r++) { for (int c = 0; c < _flex.Columns.Count; c++) { _flex[r, c] = string.Format("={0}*{1}", r + 1, c + 1); } } // add a totals row to illustrate var totRow = _flex.Rows.Count - 1; for (int c = 0; c < _flex.Columns.Count; c++) { _flex[totRow, c] = string.Format("=sum({0}1:{0}{1})", (char)('A' + c), _flex.Rows.Count - 3); } _flex.Rows[totRow].FontWeight = FontWeights.Bold; // set up event handler to update the selection status _flex.SelectionChanged += (s, e) => { var sel = _flex.Selection; // update address label var text = string.Empty; if (sel.IsValid) { text = _flex.GetAddress(sel, false); } _tbAddress.Text = text; // update formula textbox text = string.Empty; if (sel.IsValid) { var row = _flex.Rows[sel.Row] as ExcelRow; if (row != null) { var col = _flex.Columns[sel.Column]; text = row.GetDataEditor(col); } } _txtFormula.Text = text; _txtFormula.Tag = text; // update status bar text = "Ready"; if (sel.IsValid && !sel.IsSingleCell && sel.BottomRow < _flex.Rows.Count && sel.RightColumn < _flex.Columns.Count) { try { var address = _flex.GetAddress(sel, true); var avg = _flex.Evaluate(string.Format("Average({0})", address)); var cnt = _flex.Evaluate(string.Format("Count({0})", address)); var sum = _flex.Evaluate(string.Format("Sum({0})", address)); if ((double)cnt > 0) { text = string.Format("Average: {0:#,##0.##} Count: {1:n0} Sum: {2:#,##0.##}", avg, cnt, sum); } } catch { // circular reference/bad expression? } } _tbStatus.Text = text; }; // update grid when user types into formula bar _txtFormula.LostFocus += (s, e) => { var sel = _flex.Selection; if (sel.IsValid) { _txtFormula.Tag = _txtFormula.Text; _flex[sel.Row, sel.Column] = _txtFormula.Text; } }; _txtFormula.KeyDown += (s, e) => { switch (e.Key) { // apply formula when user hits enter case Key.Enter: _flex.Focus(); break; // restore original value when user hits escape case Key.Escape: _txtFormula.Text = _txtFormula.Tag as string; _txtFormula.SelectAll(); break; } }; // give grid the focus when the app loads Loaded += (s, e) => { _flex.Focus(); }; #if false // demonstrate Excel notation var val = _flex["B4"]; _flex["B4"] = "Orange"; System.Diagnostics.Debug.WriteLine("cell 'b4' was '{0}' and is now '{1}'", val, _flex["b4"]); #endif // create custom menu items CustomizeContextMenu(); // start with blue color scheme SetColorScheme(ColorScheme.Blue); }