Call the calculator in LightSwitch


Visual Studio LightSwitch is a Microsoft tool, which is used to build business applications. If you want use of the system calculator in our LightSwitch application then just follow these steps.

Step by step solution

Step 1 : Open Visual Studio LightSwitch->Go to solution explorer and click the file view.

image1.png

Step 2 : Right click on client->Add->Class.

image2.png

Step 3 : Select class->Write name (Calculator.cs)->Add.

image3.png

Code :

using System;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Automation;
namespace LightSwitchApplication
{
    public class Calculator
    {
        public static void ShowCalculator()
        {
            dynamic calc = AutomationFactory.CreateObject("WScript.Shell");
            calc.Run(@"%SystemRoot%\system32\calc.exe");
        }
    }
}

Step 4 : Now go to solution explorer then click on logical view.

image4.png

Step 5 : Right click on screens->Add screen.

image5.png

Step 6 : Select new data screen->Ok.

image6.png

Step 7 : Now add data item->Select method->Write name (ShowCalculator)->Ok.

image7.png

Step 8 : Move showcalculator to screen commandbar.

image8.png

Step 9 : Right click on showcalculator method->Edit canexecute code->Write the code.

image9.png

Code :

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Runtime.InteropServices.Automation;
namespace LightSwitchApplication
{
    public partial class CreateNew
    {
        partial void ShowCalculator_CanExecute(ref bool result)
        {
            // Write your code here.
            result = AutomationFactory.IsAvailable;
        }
    }
}

Step 10 : Again right click on showcalculator method->Edit execute code.

image10.png

Code :

using
System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Runtime.InteropServices.Automation;
namespace LightSwitchApplication
{
    public partial class CreateNew
    {
        partial void ShowCalculator_CanExecute(ref bool result)
        {
            // Write your code here.
            result = AutomationFactory.IsAvailable;
        }
        partial void ShowCalculator_Execute()
        {
            // Write your code here.
            Calculator.ShowCalculator();
        }

    }
}

Step 11 : Run application (Press F5)->Click on show calculator.

Now you will see calculator window in your LightSwitch application.

image11.png