Calculated TextBox:
Suppose you want to add a
calculated TextBox to your application. Users should be able to type
expressions into the TextBox. The expressions should be evaluated when
the control loses focus or when the user presses Enter. You could do this with
the following code:
public MainWindow()
{
InitializeComponent();
var tb = new TextBox();
tb.LostFocus +=
(s,e) => { Evaluate(s); };
tb.KeyDown += (s, e) => {
Evaluate(s); };
}
void Evaluate(object sender)
{
var tb = sender
as TextBox;
var ce = new C1.WPF.Binding.C1CalcEngine();
try
{
var value =
ce.Evaluate(tb.Text);
tb.Text =
value.ToString();
}
catch (Exception x)
{
MessageBox.Show("Error in
Expression: " +
x.Message);
}
}
Turning Grids into Spreadsheets:
Another typical
use for the C1CalcEngine
class is the implementation of calculated cells in grid controls and data
objects. We have used it to implement several samples that add spreadsheet
functionality to grid controls. The code is too long to include here, but you
can see one of the samples in action here: http://demo.componentone.com/Silverlight/ExcelBook/).