A Calculator via Visual C#
Preface
The .NET Framework provides a long list of Math APIs for calculation purposes. Our goal now is to build a calculator using some basic math operations and some of those APIs. Normally when working with Windows Forms we use a button to fire an event that warrants an event handler. But how do use the Button control to enter numeric character input into a TextBox control, and then further use other buttons to perform operations needed on the entered input? First off, there are 10, or 0 – 9 digits, that must have their text properties set to those integers to appear on the buttons. The other buttons need to have their text properties set to the operation in which they are to perform when the user clicks them. With all of those buttons, we should group them. This means that we need to use two GroupBox controls to contain the input field and the Button controls. We then need to declare some local variables, particularly to indicate the status of an operation. Sounds like a Boolean data type. We then need to write a method that calculates the total of the particular operation performed. Because there will be several different types of cases at hand, we’ll need to use the switch statement.
Examine this code that compiles as a console application:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Abs({0}) = {1}", -55, Math.Abs(-55));
Console.WriteLine("Ceiling({0}) = {1}", 55.3, Math.Ceiling(55.3));
Console.WriteLine("Pow({0},{1}) = {2}", 2, 16, Math.Pow(2, 16));
Console.WriteLine("Round({0},{1}) = {2}",
10.55358, 2, Math.Round(10.55358, 2));
Console.WriteLine("Sin({0}) = {1}", 323.333, Math.Sin(323.333));
Console.WriteLine("Cos({0}) = {1}", 323.333, Math.Cos(323.333));
Console.WriteLine("Tan({0}) = {1}", 323.333, Math.Tan(323.333));
Console.WriteLine("Sqrt({0}) = {1}", 65536, Math.Sqrt(65536));
}
}
Here we are using the .NET Framework Math APIs directly. It is obviously very simple to type in an API and plug in a value. Here is the result:
Abs(-55) = 55
Ceiling(55.3) = 56
Pow(2,16) = 65536
Round(10.55358,2) = 10.55
Sin(323.333) = 0.248414709883854
Cos(323.333) = -0.968653772982546
Tan(323.333) = -0.256453561440193
Sqrt(65536) = 256
Now if choose to get the square root of 65,536 (2 to the 16th power), we can just press a button:
I used this example because there are two possible values for a data bit: 1 or a 0. There are 8 bits in a byte. 2 raised to the 8th power = 256, which is 16 squared. 65,536 is equal to one segment of memory. Anyway, here is the code for the calculator. Try and expand on it by adding some trigonometric functions. That is the purpose of this article. This code is not split into partial classes. The downloadable file is, however. Unzip it the solution container into a newly made folder in your Projects directory, and then double-click the solution file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button23;
private System.Windows.Forms.Button button22;
private System.Windows.Forms.Button button21;
private System.Windows.Forms.Button button20;
private System.Windows.Forms.Button button19;
private System.Windows.Forms.Button button18;
private System.Windows.Forms.Button button17;
private System.Windows.Forms.Button button16;
private System.Windows.Forms.Button button15;
private System.Windows.Forms.Button button14;
private System.Windows.Forms.Button button13;
private System.Windows.Forms.Button button12;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
public Form1()
{
InitializeComponent();
}
private double valHolder1;
private double valHolder2;
private double tmpValue;
private bool hasDecimal = false;
private bool inputStatus = true;
private string calcFunc;
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtInput = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button23 = new System.Windows.Forms.Button();
this.button22 = new System.Windows.Forms.Button();
this.button21 = new System.Windows.Forms.Button();
this.button20 = new System.Windows.Forms.Button();
this.button19 = new System.Windows.Forms.Button();
this.button18 = new System.Windows.Forms.Button();
this.button17 = new System.Windows.Forms.Button();
this.button16 = new System.Windows.Forms.Button();
this.button15 = new System.Windows.Forms.Button();
this.button14 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.button12 = new System.Windows.Forms.Button();
this.button11 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
this.groupBox1.Controls.Add(this.txtInput);
this.groupBox1.Location = new System.Drawing.Point(39, 28);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(274, 34);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.txtInput.Location = new System.Drawing.Point(7, 8);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(261, 20);
this.txtInput.TabIndex = 0;
this.groupBox2.Controls.Add(this.button23);
this.groupBox2.Controls.Add(this.button22);
this.groupBox2.Controls.Add(this.button21);
this.groupBox2.Controls.Add(this.button20);
this.groupBox2.Controls.Add(this.button19);
this.groupBox2.Controls.Add(this.button18);
this.groupBox2.Controls.Add(this.button17);
this.groupBox2.Controls.Add(this.button16);
this.groupBox2.Controls.Add(this.button15);
this.groupBox2.Controls.Add(this.button14);
this.groupBox2.Controls.Add(this.button13);
this.groupBox2.Controls.Add(this.button12);
this.groupBox2.Controls.Add(this.button11);
this.groupBox2.Controls.Add(this.button10);
this.groupBox2.Controls.Add(this.button9);
this.groupBox2.Controls.Add(this.button8);
this.groupBox2.Controls.Add(this.button7);
this.groupBox2.Controls.Add(this.button6);
this.groupBox2.Controls.Add(this.button5);
this.groupBox2.Controls.Add(this.button4);
this.groupBox2.Controls.Add(this.button3);
this.groupBox2.Controls.Add(this.button2);
this.groupBox2.Controls.Add(this.button1);
this.groupBox2.Location = new System.Drawing.Point(39, 80);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(274, 223);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.button23.Location = new System.Drawing.Point(203, 186);
this.button23.Name = "button23";
this.button23.Size = new System.Drawing.Size(40, 23);
this.button23.TabIndex = 22;
this.button23.Text = "=";
this.button23.UseVisualStyleBackColor = true;
this.button23.Click += new System.EventHandler(this.button23_Click);
this.button22.Location = new System.Drawing.Point(157, 186);
this.button22.Name = "button22";
this.button22.Size = new System.Drawing.Size(39, 23);
this.button22.TabIndex = 21;
this.button22.Text = "+";
this.button22.UseVisualStyleBackColor = true;
this.button22.Click += new System.EventHandler(this.button22_Click);
this.button21.Location = new System.Drawing.Point(107, 186);
this.button21.Name = "button21";
this.button21.Size = new System.Drawing.Size(44, 23);
this.button21.TabIndex = 20;
this.button21.Text = ".";
this.button21.UseVisualStyleBackColor = true;
this.button21.Click += new System.EventHandler(this.button21_Click);
this.button20.Location = new System.Drawing.Point(57, 187);
this.button20.Name = "button20";
this.button20.Size = new System.Drawing.Size(43, 23);
this.button20.TabIndex = 19;
this.button20.Text = "+/-";
this.button20.UseVisualStyleBackColor = true;
this.button20.Click += new System.EventHandler(this.button20_Click);
this.button19.Location = new System.Drawing.Point(7, 187);
this.button19.Name = "button19";
this.button19.Size = new System.Drawing.Size(44, 23);
this.button19.TabIndex = 18;
this.button19.Text = "0";
this.button19.UseVisualStyleBackColor = true;
this.button19.Click += new System.EventHandler(this.button19_Click);
this.button18.Location = new System.Drawing.Point(203, 145);
this.button18.Name = "button18";
this.button18.Size = new System.Drawing.Size(40, 23);
this.button18.TabIndex = 17;
this.button18.Text = "1/x";
this.button18.UseVisualStyleBackColor = true;
this.button18.Click += new System.EventHandler(this.button18_Click);
this.button17.Location = new System.Drawing.Point(157, 145);
this.button17.Name = "button17";
this.button17.Size = new System.Drawing.Size(39, 23);
this.button17.TabIndex = 16;
this.button17.Text = "-";
this.button17.UseVisualStyleBackColor = true;
this.button17.Click += new System.EventHandler(this.button17_Click);
this.button16.Location = new System.Drawing.Point(107, 145);
this.button16.Name = "button16";
this.button16.Size = new System.Drawing.Size(44, 23);
this.button16.TabIndex = 15;
this.button16.Text = "3";
this.button16.UseVisualStyleBackColor = true;
this.button16.Click += new System.EventHandler(this.button16_Click);
this.button15.Location = new System.Drawing.Point(58, 145);
this.button15.Name = "button15";
this.button15.Size = new System.Drawing.Size(42, 23);
this.button15.TabIndex = 14;
this.button15.Text = "2";
this.button15.UseVisualStyleBackColor = true;
this.button15.Click += new System.EventHandler(this.button15_Click);
this.button14.Location = new System.Drawing.Point(7, 145);
this.button14.Name = "button14";
this.button14.Size = new System.Drawing.Size(44, 23);
this.button14.TabIndex = 13;
this.button14.Text = "1";
this.button14.UseVisualStyleBackColor = true;
this.button14.Click += new System.EventHandler(this.button14_Click);
this.button13.Location = new System.Drawing.Point(203, 101);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(40, 23);
this.button13.TabIndex = 12;
this.button13.Text = "Sqrt";
this.button13.UseVisualStyleBackColor = true;
this.button13.Click += new System.EventHandler(this.button13_Click);
this.button12.Location = new System.Drawing.Point(158, 101);
this.button12.Name = "button12";
this.button12.Size = new System.Drawing.Size(38, 23);
this.button12.TabIndex = 11;
this.button12.Text = "*";
this.button12.UseVisualStyleBackColor = true;
this.button12.Click += new System.EventHandler(this.button12_Click);
this.button11.Location = new System.Drawing.Point(106, 101);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(45, 23);
this.button11.TabIndex = 10;
this.button11.Text = "6";
this.button11.UseVisualStyleBackColor = true;
this.button11.Click += new System.EventHandler(this.button11_Click);
this.button10.Location = new System.Drawing.Point(58, 102);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(42, 23);
this.button10.TabIndex = 9;
this.button10.Text = "5";
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.button10_Click);
this.button9.Location = new System.Drawing.Point(7, 102);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(44, 23);
this.button9.TabIndex = 8;
this.button9.Text = "4";
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.button9_Click);
this.button8.Location = new System.Drawing.Point(202, 61);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(41, 23);
this.button8.TabIndex = 7;
this.button8.Text = "x^";
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click);
this.button7.Location = new System.Drawing.Point(157, 60);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(39, 23);
this.button7.TabIndex = 6;
this.button7.Text = "/";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
this.button6.Location = new System.Drawing.Point(106, 61);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(45, 23);
this.button6.TabIndex = 5;
this.button6.Text = "9";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
this.button5.Location = new System.Drawing.Point(58, 60);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(42, 23);
this.button5.TabIndex = 4;
this.button5.Text = "8";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
this.button4.Location = new System.Drawing.Point(7, 61);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(44, 23);
this.button4.TabIndex = 3;
this.button4.Text = "7";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
this.button3.Location = new System.Drawing.Point(190, 19);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(53, 23);
this.button3.TabIndex = 2;
this.button3.Text = "C";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
this.button2.Location = new System.Drawing.Point(106, 19);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(57, 23);
this.button2.TabIndex = 1;
this.button2.Text = "CE";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button1.Location = new System.Drawing.Point(7, 20);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "BackSpace";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
this.ClientSize = new System.Drawing.Size(338, 328);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Compute in C#";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private void button1_Click(object sender, EventArgs e)
{
string str;
int loc;
if (txtInput.Text.Length > 0)
{
str = txtInput.Text.Substring(txtInput.Text.Length - 1);
if (str == ".")
{
hasDecimal = false;
}
loc = txtInput.Text.Length;
txtInput.Text = txtInput.Text.Remove(loc - 1, 1);
}
}
private void button2_Click(object sender, EventArgs e)
{
txtInput.Text = string.Empty;
hasDecimal = false;
}
private void button3_Click(object sender, EventArgs e)
{
txtInput.Text = string.Empty;
valHolder1 = 0;
valHolder2 = 0;
calcFunc = string.Empty;
hasDecimal = false;
}
private void button4_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button4.Text;
}
else
{
txtInput.Text = button4.Text;
inputStatus = true;
}
}
private void button5_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button5.Text;
}
else
{
txtInput.Text = button5.Text;
inputStatus = true;
}
}
private void button6_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button6.Text;
}
else
{
txtInput.Text = button6.Text;
inputStatus = true;
}
}
private void CalculateTotals()
{
valHolder2 = System.Double.Parse(txtInput.Text);
switch (calcFunc)
{
case "Add":
valHolder1 = valHolder1 + valHolder2;
break;
case "Subtract":
valHolder1 = valHolder1 - valHolder2;
break;
case "Divide":
valHolder1 = valHolder1 / valHolder2;
break;
case "Multiply":
valHolder1 = valHolder1 * valHolder2;
break;
case "PowerOf":
valHolder1 = System.Math.Pow(valHolder1, valHolder2);
break;
}
txtInput.Text = valHolder1.ToString();
inputStatus = false;
}
private void button7_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
if (calcFunc == string.Empty)
{
valHolder1 = System.Double.Parse(txtInput.Text);
txtInput.Text = string.Empty;
}
else
{
CalculateTotals();
}
calcFunc = "Divide";
hasDecimal = false;
}
}
private void button8_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
if (calcFunc == string.Empty)
{
valHolder1 = System.Double.Parse(txtInput.Text);
txtInput.Text = string.Empty;
}
else
{
CalculateTotals();
}
calcFunc = "PowerOf";
hasDecimal = false;
}
}
private void button9_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button9.Text;
}
else
{
txtInput.Text = button9.Text;
inputStatus = true;
}
}
private void button10_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button10.Text;
}
else
{
txtInput.Text = button10.Text;
inputStatus = true;
}
}
private void button11_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button11.Text;
}
else
{
txtInput.Text = button11.Text;
inputStatus = true;
}
}
private void button12_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
if (calcFunc == string.Empty)
{
valHolder1 = System.Double.Parse(txtInput.Text);
txtInput.Text = string.Empty;
}
else
{
CalculateTotals();
}
calcFunc = "Multiply";
hasDecimal = false;
}
}
private void button13_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
tmpValue = System.Double.Parse(txtInput.Text);
tmpValue = System.Math.Sqrt(tmpValue);
txtInput.Text = tmpValue.ToString();
hasDecimal = false;
}
}
private void button14_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button14.Text;
}
else
{
txtInput.Text = button14.Text;
inputStatus = true;
}
}
private void button15_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button15.Text;
}
else
{
txtInput.Text = button15.Text;
inputStatus = true;
}
}
private void button16_Click(object sender, EventArgs e)
{
if (inputStatus)
{
txtInput.Text += button16.Text;
}
else
{
txtInput.Text = button16.Text;
inputStatus = true;
}
}
private void button17_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
if (calcFunc == string.Empty)
{
valHolder1 = System.Double.Parse(txtInput.Text);
txtInput.Text = string.Empty;
}
else
{
CalculateTotals();
}
calcFunc = "Subtract";
hasDecimal = false;
}
}
private void button18_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
tmpValue = System.Double.Parse(txtInput.Text);
tmpValue = 1 / tmpValue;
txtInput.Text = tmpValue.ToString();
hasDecimal = false;
}
}
private void button19_Click(object sender, EventArgs e)
{
if (inputStatus)
{
if (txtInput.Text.Length >= 1)
{
txtInput.Text += button19.Text;
}
}
}
private void button20_Click(object sender, EventArgs e)
{
if (inputStatus)
{
if (txtInput.Text.Length > 0)
{
valHolder1 = -1 * System.Double.Parse(txtInput.Text);
txtInput.Text = valHolder1.ToString();
}
}
}
private void button21_Click(object sender, EventArgs e)
{
if (inputStatus)
{
if (!hasDecimal)
{
if (txtInput.Text.Length != 0)
{
if (txtInput.Text != "0")
{
txtInput.Text += button21.Text;
hasDecimal = true;
}
}
else
{
txtInput.Text = "0.";
}
}
}
}
private void button22_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0)
{
if (calcFunc == string.Empty)
{
valHolder1 = System.Double.Parse(txtInput.Text);
txtInput.Text = string.Empty;
}
else
{
CalculateTotals();
}
calcFunc = "Add";
hasDecimal = false;
}
}
private void button23_Click(object sender, EventArgs e)
{
if (txtInput.Text.Length != 0 && valHolder1 != 0)
{
CalculateTotals();
calcFunc = string.Empty;
hasDecimal = false;
}
}
}
Notice string.Empty. Here is System.Entry disassembled:
[Serializable]
internal sealed class Empty : ISerializable
{
public static readonly Empty Value = new Empty();
private Empty()
{
}
[SecurityCritical]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException("info");
}
UnitySerializationHolder.GetUnitySerializationInfo(info, 1, null, null);
}
public override string ToString()
{
return string.Empty;
}
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.