Performing calculations using a class and referencing form controls
<p>I am trying to learn classes, meaning writing codes outside of the windows form and calling that code in the form. I created a basic calculation in the form and it works. The problem is, I want to know how I can move the entire calculation into a class and call it into the form on load or when a control is click. It does not work!</p>

<p><strong>Here is my local or form version which works:</strong>
Please note that the form will load with the first calculation because the Radiobutton is checked on load.</p>

<pre><code>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 Calculations
{
    public partial class Calculations : Form
    {
        public Calculations()
        {
            InitializeComponent();
        }

        public  decimal SalesTax = 0.065M;
        public  decimal AppliedTax;
        public  decimal Price;
        public  decimal SubTotal;
        public  decimal GrandTotal;


        public object Calc()
        {
            Int32 GetQuantity = Convert.ToInt32(txtQuantity.Text);
            SubTotal = (Price * GetQuantity);
            AppliedTax = (SubTotal * SalesTax);
            GrandTotal = (SubTotal + AppliedTax);

            if (radRed.Checked == true)
            {
                Price = 100;
            }

            else if (radBlue.Checked == true)
            {
                Price = 200;
            }

            else if (radGreen.Checked == true)
            {
                Price = 300;
            }

            lblPrice.Text = GrandTotal.ToString("c");

            return GrandTotal;
        }


        private void Calculations_Load(object sender, EventArgs e)
        {
            txtQuantity.Text = "10";
            radRed.Checked = true;
            Calc();

        }

        private void radRed_CheckedChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void radBlue_CheckedChanged(object sender, EventArgs e)
        {   
            Calc();

        }

        private void radGreen_CheckedChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {  
          Calc();
        }
    }
}
</code></pre>

<p>And here is my attempt to place all of this in a class:
It works but not on load, you have to check another radiobutton first</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calculations
{
    class ClsCalc
    {
        class ClsRef
        {
            public static Calculations FormCtrls
            {
                get
                {
                    return Calculations.ActiveForm as Calculations;
                }
            }
        }

        public static decimal SalesTax = 0.065M;
        public static decimal AppliedTax;
        public static decimal Price;
        public static decimal SubTotal;
        public static decimal GrandTotal;


        public static object Calc()
        {
            Int32 GetQuantity = Convert.ToInt32(ClsRef.FormCtrls.txtQuantity.Text);
            SubTotal = (Price * GetQuantity);
            AppliedTax = (SubTotal * SalesTax);
            GrandTotal = (SubTotal + AppliedTax);

            if (ClsRef.FormCtrls.radRed.Checked == true)
            {
                Price = 100;
            }

            else if (ClsRef.FormCtrls.radBlue.Checked == true)
            {
                Price = 200;
            }

            else if (ClsRef.FormCtrls.radGreen.Checked == true)
            {
                Price = 300;
            }

            ClsRef.FormCtrls.lblPrice.Text = GrandTotal.ToString("c");

            return GrandTotal;
        }
    }
}
</code></pre>

<p>Finally I called it in the form Load event:</p>

<blockquote>
  <p>ClsCalc.Calc();</p>
</blockquote>

<p>Please note this is not a real project just a way to learn, well for me that is.</p>

<p>Thank you!</p>
