I'm pressing Ctrl+F5 to run it and its coming up, with all the buttons in place but nothing happens when i press them.
Positive ive followed the code right to the letter.
But ive probably made another rookie mistake:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// variables to hold operands
private double valHolder1;
private double valHolder2;
// variable to hold temporary values
private double tmpValue;
// True if '.' is use else false
private bool hasDecimal = false;
private bool inputStatus = true;
// variable to hold Operator
private string calcFunc;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button1.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button1.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
//Due to a mix up when creating the form, button4 is actually number 2
private void button4_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button4.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button4.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
//and button6 is nimber 3
private void button6_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button6.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button6.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
// button32 is number 4
private void button32_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button32.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button32.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
// button7 is number 5
private void button7_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button7.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button7.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
// button5 is number6
private void button5_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button5.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button5.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
// button8 is number 7
private void button8_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button8.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button8.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
// button3 is number 8
private void button3_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button3.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button3.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
private void button9_Click(object sender, System.EventArgs e)
{
//Check the inputStatus
if (inputStatus)
{
//Its True
//Append values to the value
//in the input box
textBox1.Text += button9.Text;
}
else
{
//Value is False
//Set the value to the value of the button
textBox1.Text = button9.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
// The number zero has slightly different code to the other
// 9 numbers as we don't want to be able to write a zero as
// the first number.
private void button10_Click(object sender, System.EventArgs e)
{
//Check the input Status
if (inputStatus)
{
//If True
//Now check to make sure the
//input Box has a value
if (textBox1.Text.Length >= 1)
{
//Add our zero
textBox1.Text += button10.Text;
}
}
}
private void button11_Click(object sender, System.EventArgs e)
{
//Check the input status
if (inputStatus)
{
//Check that it already has a decimal, if it does: do nothing.
if (!hasDecimal)
{
//Check to make sure the length is > 1
//Dont want user to add decimal as first charachter
if (textBox1.Text.Length != 0)
{
//make sure zero isnt the first number
if (textBox1.Text != "0")
{
//It met all requirements so add the zero
textBox1.Text += button11.Text;
//Toggle the flag to true (only one decimal per calculation)
hasDecimal = true;
}
}
else
{
//Since the length isnt > 1
//make the text = 0.
textBox1.Text = "0.";
}
}
}
}
private void button16_Click(object sender, System.EventArgs e)
{
//Make sure input box has a value
if (textBox1.Text.Length != 0)
{
//Check the values of the function flag
if (calcFunc == string.Empty)
{
//Flag is empty
//Assign the value in the input
//box to our holder
valHolder1 = System.Double.Parse(textBox1.Text);
//Empty the input box
textBox1.Text = string.Empty;
}
else
{
//Flag isnt empty
//Call the CalculateTotals method
CalculateTotals();
}
//Assign a value to the calc function flag
calcFunc = "Add";
//Toggle the decimal flag
hasDecimal = false;
}
}
private void button17_Click(object sender, System.EventArgs e)
{
//Make sure the input box has a value
if(textBox1.Text.Length != 0)
{
//Check the value of the calculate function flag
if (calcFunc == string.Empty)
{
//Flag is empty
//Assign the value
//box to the holder
valHolder1 = System.Double.Parse(textBox1.Text);
//Empty the input box
textBox1.Text = string.Empty;
}
else
{
//Flag isnt empty
//Call the calculate totals method
CalculateTotals();
}
//assign a value to the
//calculate function flag
calcFunc = "Subtract";
//Toggle the decimal flag
hasDecimal = false;
}
}
private void button18_Click(object sender, System.EventArgs e)
{
//Make sure the input box has a value
if (textBox1.Text.Length != 0)
{
//Check the value of the calculate function flag
if (calcFunc == string.Empty)
{
//Flag is empty
//Assign the value
//box to the holder
valHolder1 = System.Double.Parse(textBox1.Text);
//Empty the input box
textBox1.Text = string.Empty;
}
else
{
//Flag isnt empty
//Call the calculate totals method
CalculateTotals();
}
//assign a value to the
//calculate function flag
calcFunc = "Multiply";
//Toggle the decimal flag
hasDecimal = false;
}
}
private void button19_Click(object sender, System.EventArgs e)
{
//Make sure the input box has a value
if (textBox1.Text.Length != 0)
{
//Check the value of the calculate function flag
if (calcFunc == string.Empty)
{
//Flag is empty
//Assign the value
//box to the holder
valHolder1 = System.Double.Parse(textBox1.Text);
//Empty the input box
textBox1.Text = string.Empty;
}
else
{
//Flag isnt empty
//Call the calculate totals method
CalculateTotals();
}
//assign a value to the
//calculate function flag
calcFunc = "Divide";
//Toggle the decimal flag
hasDecimal = false;
}
}
private void button20_Click(object sender, System.EventArgs e)
{
//Make sure the input biox has a value
if (textBox1.Text.Length != 0)
{
//Check the value of the calculate function flag
if (calcFunc == string.Empty)
{
//Flag is empty
//Assign the value
//box to the holder
valHolder1 = System.Double.Parse(textBox1.Text);
//Empty the input box
textBox1.Text = string.Empty;
}
else
{
//Flag isnt empty
//Call the calculate totals method
CalculateTotals();
}
//assign a value to the
//calculate function flag
calcFunc = "PowerOf";
//Toggle the decimal flag
hasDecimal = false;
}
}
private void button22_Click(object sender, System.EventArgs e)
{
//Make sure the input has a value
if (textBox1.Text.Length != 0)
{
//Assign the variable the value in the input box
tmpValue = System.Double.Parse(textBox1.Text);
//Perform the square root
tmpValue = System.Math.Sqrt(tmpValue);
//Display the results in the input box
textBox1.Text = tmpValue.ToString();
//Clear the deciaml flag
hasDecimal = false;
}
}
private void button15_Click(object sender, System.EventArgs e)
{
//declare locals needed
string str;
int loc;
//Make sure the input has a value
if (textBox1.Text.Length != 0)
{
//get the next to last charachter
str = textBox1.Text.Substring(textBox1.Text.Length - 1);
// Check if it has a decimal
if (str == ".")
{
//Toggle the hasDecimal flag
hasDecimal = false;
}
//Get the length of the string
loc = textBox1.Text.Length;
//Remove the last charachter of the string
textBox1.Text = textBox1.Text.Remove(loc - 1, 1);
}
}
private void button13_Click(object sender, System.EventArgs e)
{
//Empty the input box
textBox1.Text = string.Empty;
//Toggle the deciaml flag
hasDecimal = false;
}
private void button14_Click(object sender, System.EventArgs e)
{
//clear the input box
textBox1.Text = string.Empty;
//Set the valHolders to 0
valHolder1 = 0;
valHolder2 = 0;
//Set the calcFunc switch to empty
calcFunc = string.Empty;
//Toggle the decimal flag;
hasDecimal = false;
}
private void CalculateTotals()
{
valHolder2 = System.Double.Parse(textBox1.Text);
//determine which calculation to execute
//by checking the value of calcFunc
switch (calcFunc)
{
//addition
case "Add":
valHolder1 = valHolder1 + valHolder2;
break;
//subtraction
case "Subtract":
valHolder1 = valHolder1 - valHolder2;
break;
//multiply
case "Multiply":
valHolder1 = valHolder1 * valHolder2;
break;
//division
case "Divide":
valHolder1 = valHolder1 / valHolder2;
break;
//Power of
case "PowerOf":
valHolder1 = System.Math.Pow(valHolder1, valHolder2);
break;
}
//set our input area to the value of the calculation
textBox1.Text = valHolder1.ToString();
inputStatus = false;
}
}
}
Gyah.






MultiQuote







|