Windows Form Calculator
At first it was a challenge to build this calculator, but I found a good example out there and did it. I am overwhelmed of the amount of logic and obstacles that went into creating this Windows form, but I finally triumphed. I seized the day with this.
I had no reference material I could look at, but my computer at home. I was simply counting a programming book to search for answers, but I got none. I am waiting for my C# book to arrive and I specifically ventured out today to find some kind of reference material at a bookstore, but I couldn't find a good book. I had realized that relying on bookstores could be a dead end. Now that I found an answer by google, I got my answer. Yes!
This is just the beginning of the many applications that I try to overcome. I plan to plug away and practice more to sharpen my C# programming skills.
Follow this code in your Visual Studio program and you will be on your way.
Here is the sample 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 Calculator2
{
public partial class Calculator : Form
{
Double value = 0;
string operation = "0";
bool operation_pressed = false;
public Calculator()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
if ((result.Text == "0")|| (operation_pressed))
{
result.Clear();
}
Button b = (Button)sender;
result.Text = result.Text + b.Text;
}
private void button18_Click(object sender, EventArgs e)
{
result.Text = "0";
}
private void operator_click(object sender, EventArgs e)
{
operation_pressed = false;
Button b = (Button)sender;
operation = b.Text;
value = Double.Parse(result.Text);
operation_pressed = true;
equation.Text = value + " " + operation;
}
private void button16_Click(object sender, EventArgs e)
{
equation.Text = "";
switch (operation)
{
case "+":
result.Text = (value + Double.Parse(result.Text)).ToString();
break;
case "-":
result.Text = (value - Double.Parse(result.Text)).ToString();
break;
case "*":
result.Text = (value * Double.Parse(result.Text)).ToString();
break;
case "/":
result.Text = (value / Double.Parse(result.Text)).ToString();
break;
default:
break;
}//end switch
}
private void button17_Click(object sender, EventArgs e)
{
result.Text = "0";
value = 0;
}
}
}
No comments:
Post a Comment