Skip to main content Skip to footer

30 lines code make a calculator by Spread

Can 30 lines of code make a calculator? Yes, because we have Spread. Spread XAML controls (Spread WinRT and Spread WPF-Silverlight) bring all the data visualization and calculation features. Create custom calculators, dynamic interactive dashboards, rich colorful reports, and much more! The rich API for Spread provides a complete object model for the spreadsheet document, and a rich set of user interface events which your application can handle to customize the user experience. Spread XAML controls include the powerful Spread Designer tool, which enables you to create and design your spreadsheet interfaces much like you would in a spreadsheet program like Excel, entirely in design time and without writing any code. Your changes are saved to a spreadsheet template that is automatically added to your project and loaded into the control in run time. This is a sample demonstrate how to use Spread Designer and few lines of code to make a calculator, it is very easy and simple. (Here use Spread WinRT as sample) Firstly, create a Window Store App project in VS2012 And then drag GcSpreadSheet control from the toolbox to design surface And right click on the control, choose "Create a new document" menu item in context menu, then a document name is spreasheet1.ssxaml will be created and set it to DocumentUri property of control automatically After this, the Designer will popup automatically, then you can design appearance of your calculator, it is very easy just like using Excel. You may layout cells by reisze columns and rows, merge cells, set borders, color and font for cells, hide unnecessary parts like scroll bars, tab strip, grid line, row/column headers and protect sheet to avoid cell editing by end-user... save the template and exist Designer, that's it. Next is just wire the CellClick event and write few lines of code in the event processor XAML code:


<UI:GcSpreadSheet x:Name="gcSpreadSheet1" Width="490" Height="730" DocumentUri="ms-appx:///Spread Documents/spreadsheet1.ssxml" CellClick="gcSpreadSheet1_CellClick"/>  

C# code:


void gcSpreadSheet1_CellClick(object sender, CellClickEventArgs e)  
{  
    var sheet = this.gcSpreadSheet1.ActiveSheet;  
    if (e.Column == 4 && e.Row == 7) //=  
    {  
        sheet[2, 1].Formula = "";  
        try  
        {  
            sheet[2, 1].Formula = sheet[1, 1].Text;  
        }  
        catch  
        {  
            sheet[2, 1].Text = "Invalid Formula!";  
        }  
    }  
    else if (e.Column == 1 && e.Row == 3) //C  
    {  
        sheet[1, 1].ResetText();  
        sheet[2, 1].ResetText();  
        sheet[2, 1].Formula = "";  
    }  
    else if (e.Column == 3 && e.Row == 3) //BS  
    {  
        var txt = sheet[1, 1].Text;  
        if (txt == string.Empty) return;  
        sheet[1, 1].Text = txt.Substring(0, txt.Length - 1);  
    }  
    else if (e.Column > 0 && e.Column < 5 && e.Row > 3 && e.Row < 9) //digits and signs  
    {  
        sheet[1, 1].Text = sheet[1, 1].Text + sheet[e.Row, e.Column].Text;  
        sheet[2, 1].ResetText();  
        sheet[2, 1].Formula = "";  
    }  
}  

Go and run your application, your calculator works Because Spread WPF-Silverlight and Spread WinRT are same API and data model, you can share and reuse the template file and code in your WPF, Silverlight and Windows Store App projects without any change. Go and get sample code here. To know more information about Spread XAML control, visit web page of Spread WPF-Silverlight and Spread WinRT.

MESCIUS inc.

comments powered by Disqus