Reader Level:
ARTICLE

Making a calculator in Windows Application using Web Service

Posted by A K Articles | Web Services in C# December 20, 2011
In this article you will learn how to create a Windows Application to perform arithmetic operation like a calculator in a web service.
  • 1
  • 0
  • 22291

Introduction

I am creating a web service for performing arithmetic operations or say a calculator. Then, I will use this service in a windows based application to perform arithmetic operations like a calculator. So, let's create a web service. Follow the given steps.

  • Create new project
  • Select ASP.NET Empty Web Application
  • Give a name to your project and click ok button
  • Go to Solution Explorer and right click at your project
  • Select Add New Item and select Web Service application
  • Give it name and click ok button 
Now replace the code on .asmx.cs page with the following code.

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Services;

namespace
calc
{
    /// <summary>
    /// Summary description for CalcWebService
    /// </summary>
    [WebService(Namespace = "mycalculatorexample.org")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class CalcWebService : System.Web.Services.WebService
    {
       [WebMethod]
        public string calculate(string first, string second, char sign)
        {
            string result;
            switch (sign)
            {
                case '+':
                    {
                        result = (Convert.ToInt32(first) + Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '-':
                    {
                        result = (Convert.ToInt32(first) - Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '*':
                    {
                        result = (Convert.ToInt32(first) * Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '/':
                    {
                        result = (Convert.ToInt32(first) / Convert.ToInt32(second)).ToString();
                        break;
                    }
                case '%':
                    {
                        result = (Convert.ToInt32(first) % Convert.ToInt32(second)).ToString();
                        break;
                    }
                default:
                    result = "Invalid";
                    break;
            }
            return result;
        }
    }
}
 
Run the application.

Output:


Go to test page by clicking at "calculate" -> do the entry and click the "invoke" button.
 

It will show the result like the below figure.( After clicking the "invoke" button)



Similarly, you can perform different operations like addition, subtraction, multiplication, division and modulo division. Now we consume this web service into window form application.

Create a windows application and add a service reference ( You can get help here ). After adding the reference, go to the design page and arrange the UI controls like in the below figure.



Now, write the following code in the .cs file.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
 

namespace
Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string i, j;
        static char c;
        private void btnthree_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(3);
        }
        private void btnone_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(1);
        }
        private void btntwo_Click(object sender, EventArgs e)
       {
            textBox1.Text += Convert.ToString(2);
        } 
        private void btnfour_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(4);
        } 
        private void btnfive_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(5);
        } 
        private void btnsix_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(6);
        } 
        private void btnseven_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(7);
        } 
        private void btneight_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(8);
        } 
        private void btnnine_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(9);
        } 
        private void btnzero_Click(object sender, EventArgs e)
        {
            textBox1.Text += Convert.ToString(0);
        } 
        private void btnplus_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '+';
        }
        private void btnminuse_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '-';
        }
        private void btnmultiply_Click(object sender, EventArgs e)
        {
           i = textBox1.Text;
            textBox1.Text = "";
            c = '*';
        }
        private void btndivide_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '/';
        }
        private void btnmodulo_Click(object sender, EventArgs e)
        {
            i = textBox1.Text;
            textBox1.Text = "";
            c = '%';
        }
        private void btnequal_Click(object sender, EventArgs e)
        {
            j = textBox1.Text;
            calclocalhost.CalcWebService obj = new calclocalhost.CalcWebService();
            textBox1.Text = obj.calculate(i, j, c);
        }
    }
}

Run the application.

Output:



You can work as calculator.

Article Extensions
Contents added by Chiranjeevi on Aug 06, 2012
Download File: Windowstoweb.zip
COMMENT USING

Trending up