Home  • Programming • C#.NET

Consume WCF calculator service by winform

using-calclient-calservice-system-collections
using CalClient.CalService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CalClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DataTable table = new DataTable();
            table.Columns.Add("operator", typeof(string));
            table.Columns.Add("name", typeof(string));


            // Here we add five DataRows.
            table.Rows.Add("+", "Sum");
            table.Rows.Add("-", "Subtract");
            table.Rows.Add("/", "Divide");
            table.Rows.Add("*", "Multiply");

            cmbOperator.DataSource = table;
            cmbOperator.DisplayMember = "name";
            cmbOperator.ValueMember = "operator";

            foreach (TextBox tb in this.Controls.OfType<TextBox>().Where(x => x.CausesValidation == true))
            {
                tb.Validating += textBox_Validating;
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            CalServiceClient client = new CalServiceClient();
           
            
            double a = Convert.ToDouble(txtA.Text);
            double b = Convert.ToDouble(txtB.Text);
            string oparator = cmbOperator.SelectedValue.ToString();

            if (oparator == "+")
            {

                txtDisplay.Text = client.add(a, b).ToString();

            }
            else if (oparator == "-")
            {

                txtDisplay.Text = client.sub(a, b).ToString();

            }
            else if (oparator == "/")
            {

                txtDisplay.Text = client.div(a, b).ToString();

            }
            else if (oparator == "*")
            {

                txtDisplay.Text = client.mul(a, b).ToString();

            }
            
            
        }

        private void textBox_Validating(object sender, CancelEventArgs e)
        {
           TextBox currenttb = (TextBox)sender;
           if(currenttb.Text == ""){
              MessageBox.Show(string.Format("Empty field {0 }",currenttb.Name.Substring(3)));
             e.Cancel = true;
           }else {
            e.Cancel = false;
          }
       }

      
    }

}

Comments 1


<service name="WcfCalService.CalService">
        <endpoint address="CalService" binding="basicHttpBinding" bindingConfiguration=""
          name="CalService" contract="WcfCalService.ICalService" />       
      </service>

Share

Copyright © 2024. Powered by Intellect Software Ltd