C# Programming - Basic Calculator


C# Programming - Basic Calculator.
[Video Demonstration and Source Code]

🎓 Master Visual Basic .NET and Access Database By Building the Point Of Sale System (POS).
ðŸ“ē Enroll Now: https://bit.ly/2WcbRhX

Visual C# Programming
» How to Create a Basic Calculator Using struct.

[Screenshot]


What you will learn in this video tutorial.
  • Creating and Using Code File in C#
  • Using struct in C# .NET Project
  • Creating if-else if statement
  • Using switch statement (switch case)
  • Creating an array of buttons (Adding EventHandler)
  • Constructing for statement (for loop)
  • Using basic arithmetic operators in C#
  • {
  • -              /, Obelus (Division)
  • -              *, Times (Multiplication)
  • -              -, Minus (Subtraction)
  • -              +, Plus (Addition)
  • }
  • Setting Form Caption Programmatically
  • Changing the Form Title Text Programmatically
  • Adding Comments in C# Code (Single-Line and Multiple-Lines)
  • And much more!!!

#YouTube
» https://youtu.be/q_Ek6CQr6hk



Thank you for watching
» Don't Forget to Like, Comment, Share and Subscribe to my Channel.

[Source Code]

1.> Calculator.cs [Add Code File]

public struct Calculator
{
    private double _a;
    private double _b;

    public double a
    {
        get { return _a; }
        set { _a = value; }
    }

    public double b
    {
        get { return _b; }
        set { _b = value; }
    }

    public double add()
    {
        return _a + _b;
    }

    public double subtract()
    {
        return _a - _b;
    }

    public double multiply()
    {
        return _a * _b;
    }

    public double divide()
    {
        return _a / _b;
    }
}

2.> Form1.cs

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 myCalc
{
    public partial class Form1 : Form
    {
        private string _op;
        private double _r = 0.0;
        private Button[] _btn;
        private const string _cam = "Visual C# - Basic Calcultor (Struct) - Code A Minute";
        public Form1()
        {
            InitializeComponent();
            this.Text = _cam;
            _btn = new Button[4] { btnAdd, btnSubtract, btnMultiply, btnDivide };
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for(int i = 0; i < 4; i++)
            {
                this._btn[i].ForeColor = Color.Red;
                this._btn[i].Click += new EventHandler(this.ShowOperator);
            }
        }

        private void ShowOperator(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (btn.Text == "+")
            {
                _op = "+";
            }
            else if(btn.Text == "-")
            {
                _op = "-";
            }
            else if(btn.Text == "x")
            {
                _op = "x";
            }
            else if(btn.Text == "/")
            {
                _op = "/";
            }
            LabelOperator.Text = _op;
        }

        private void btnCalc_Click(object sender, EventArgs e)
        {
            Calculator c = default(Calculator);
            c.a = Convert.ToDouble(txtNum1.Text.Trim());
            c.b = Convert.ToDouble(txtNum2.Text.Trim());

            switch(_op)
            {
                case "+":
                    _r = c.add();
                    break;
                case "-":
                    _r = c.subtract();
                    break;
                case "x":
                    _r = c.multiply();
                    break;
                case "/":
                    _r = c.divide();
                    break;
                default:
                    break;
            }
            txtResult.Text = c.a + " " +
                _op + " " +
                c.b + " = " +
                _r.ToString("F2");
                // Thanks so much for watching my video
                // i hope you enjoy my video
                // thanks once again.
                /*
                    Please Like, Comment and subscribe to my channel.
                    thanks.
                */
        }
    }
}

Visit me @:
» https://www.facebook.com/CodeAMinute

#CodeAMinute #ibasskung #CSharp #Programming #Calculator #YouTube #structure #struct

Comments