So i have a working calculator written in C# (complete code included below) and i can only input numbers and symbols using the calculators buttons. I would like to be able to use the keyboard and the buttons as input devices. My problem is i dont know how since i have only been learning c# for a week. I would like one of you who are much more experienced and smarter than me to write that code :) Complete calculator program included and thank you in advanced.
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "1";
this.textBox1.Text += input;
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "2";
this.textBox1.Text += input;
}
private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "3";
this.textBox1.Text += input;
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "4";
this.textBox1.Text += input;
}
private void button5_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "5";
this.textBox1.Text += input;
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "6";
this.textBox1.Text += input;
}
private void button7_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "7";
this.textBox1.Text += input;
}
private void button8_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "8";
this.textBox1.Text += input;
}
private void button9_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
private void button10_Click(object sender, EventArgs e)
{
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
if (operation == '+')
{
result = num1 + num2;
textBox1.Text = result.ToString();
}
else if (operation == '-')
{
result = num1 - num2;
textBox1.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox1.Text = result.ToString();
}
else if (operation == '/')
{
if (num2 != 0)
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "undefined";
}
}
}
private void button11_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
}
private void button12_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "0";
this.textBox1.Text += input;
}
private void button13_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '*';
input = string.Empty;
this.textBox1.Text += input;
}
private void button14_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '+';
input = string.Empty;
this.textBox1.Text += input;
}
private void button15_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '-';
input = string.Empty;
this.textBox1.Text += input;
}
private void button16_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '/';
input = string.Empty;
this.textBox1.Text += input;
}
}
}
Look into the KeyDown event: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx
... or the KeyPress event (depending on how you want to handle things): https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).aspx
...and Keys enumeration: https://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx
You'll need to handle the KeyDown event appropriately (or KeyPress if you choose that), as shown in the example below:
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.NumPad0){
// do something
}
}
Don't forget to subscribe to the event:
this.KeyDown += Form1_KeyDown;
Create a KeyPressEventHandler and assign a method
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Form1()
{
InitializeComponent();
this.KeyPress +=
new KeyPressEventHandler(Form1_KeyPress);
}
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
//keypressed
System.Console.WriteLine(e.KeyChar);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "1";
this.textBox1.Text += input;
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "2";
this.textBox1.Text += input;
}
private void button3_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "3";
this.textBox1.Text += input;
}
private void button4_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "4";
this.textBox1.Text += input;
}
private void button5_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "5";
this.textBox1.Text += input;
}
private void button6_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "6";
this.textBox1.Text += input;
}
private void button7_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "7";
this.textBox1.Text += input;
}
private void button8_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "8";
this.textBox1.Text += input;
}
private void button9_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
private void button10_Click(object sender, EventArgs e)
{
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
if (operation == '+')
{
result = num1 + num2;
textBox1.Text = result.ToString();
}
else if (operation == '-')
{
result = num1 - num2;
textBox1.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox1.Text = result.ToString();
}
else if (operation == '/')
{
if (num2 != 0)
{
result = num1 / num2;
textBox1.Text = result.ToString();
}
else
{
textBox1.Text = "undefined";
}
}
}
private void button11_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
}
private void button12_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "0";
this.textBox1.Text += input;
}
private void button13_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '*';
input = string.Empty;
this.textBox1.Text += input;
}
private void button14_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '+';
input = string.Empty;
this.textBox1.Text += input;
}
private void button15_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '-';
input = string.Empty;
this.textBox1.Text += input;
}
private void button16_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
operand1 = input;
operation = '/';
input = string.Empty;
this.textBox1.Text += input;
}
}
}
To get which key was pressed use
e.KeyChar
Related
I'm Making a Calculator. I have created the code when I input two numbers and press equal it does its job such as subtracting, adding, multiplying and dividing. help me to make the code for subtracting, multiplying and dividing continuously without pressing equal for addition i have done the code please help me for the buttons. I will include the whole code below please help me fix it.
namespace WindowsFormsApp1
{
public partial class Windows10Calcoo : Form
{
string Input, NumStore1, NumStore2 = string.Empty ;
char Operation;
double Answ;
public Windows10Calcoo()
{
InitializeComponent();
}
private void PlusBtn_Click(object sender, EventArgs e)
{
//Creating + and Storing Num1
NumStore1 = Input;
Operation = '+';
Input = string.Empty;
//Storing Number 2
NumStore2 = Input;
double num1, num2;
double.TryParse(NumStore1, out num1);
double.TryParse(NumStore2, out num2);
// Adding Code
Answ += num1 + num2;
textBox1.Text = Answ.ToString();
}
private void SubBtn_Click(object sender, EventArgs e)
{
NumStore1 = Input;
Operation = '-';
Input = string.Empty;
//Storing Number 2
NumStore2 = Input;
double num1, num2;
double.TryParse(NumStore1, out num1);
double.TryParse(NumStore2, out num2);
// Adding Code
Answ += num1 - num2 ;
textBox1.Text = Answ.ToString();
}
private void DivideBtn_Click(object sender, EventArgs e)
{
NumStore1 = Input;
Operation = '/';
Input = string.Empty;
}
private void MultiBtn_Click(object sender, EventArgs e)
{
NumStore1 = Input;
Operation = '*';
Input = string.Empty;
}
private void BackSpaceBtn_Click(object sender, EventArgs e)
{
int textlength = textBox1.Text.Length;
textBox1.Text = textBox1.Text.Substring(0, textlength - 1);
}
private void OneBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "1";
this.textBox1.Text += Input;
}
private void TwoBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "2";
this.textBox1.Text += Input;
}
private void ThreeBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "3";
this.textBox1.Text += Input;
}
private void FourBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "4";
this.textBox1.Text += Input;
}
private void FiveBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "5";
this.textBox1.Text += Input;
}
private void SixBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "6";
this.textBox1.Text += Input;
}
private void SevenBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "7";
this.textBox1.Text += Input;
}
private void EightBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "8";
this.textBox1.Text += Input;
}
private void NineBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "9";
this.textBox1.Text += Input;
}
private void ZeroBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += "0";
this.textBox1.Text += Input;
}
private void DecimalBtn_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
Input += ".";
this.textBox1.Text += Input;
}
private void ClearBtn_Click(object sender, EventArgs e)
{
Input += "C";
this.textBox1.Text = "";
this.Input = string.Empty;
this.NumStore1 = string.Empty;
this.NumStore2 = string.Empty;
Answ = 0;
}
private void EqualBtn_Click(object sender, EventArgs e)
{
NumStore2 = Input;
double num1, num2;
double.TryParse(NumStore1, out num1);
double.TryParse(NumStore2, out num2);
this.textBox1.Text = "";
this.Input = string.Empty;
this.NumStore1 = string.Empty;
this.NumStore2 = string.Empty;
if (Operation == '+')
{
Answ = num1 + num2;
textBox1.Text = Answ.ToString();
}
else if (Operation == '-')
{
Answ = num1 - num2;
textBox1.Text = Answ.ToString();
}
else if (Operation == '*')
{
Answ = num1 * num2;
textBox1.Text = Answ.ToString();
}
else if (Operation == '/')
{
if (num2 != 0)
{
Answ = num1 / num2;
textBox1.Text = Answ.ToString();
}
else
{
textBox1.Text = "DIV/Zero!";
}
}
}
private void Windows10Calcoo_Load(object sender, EventArgs e)
{
}
}
}
for subtraction button its not subtracting sits continuesly adding when i use [Answ += num1 - num2 ;], if i use [Answ -= num1 - num2 ;] its adding negatively .
I am a programming noob and I can't seem to get this calculator working right. I have already coded all the buttons and allowed keyboard input(which i have not put into the code excerpt because its irrelevent) from the user but like the title suggests, the equals button will not work for more than one sum. I'm not sure what's causing the problem. Any suggestions would be appreciated.
Here's my code.
public partial class MainWindow : Window
{
string input = string.Empty; //String storing user input
string operand1 = string.Empty; //String storing first operand
string operand2 = string.Empty; //String storing second operand
char operation; //char for operarion
double result = 0.0; //calculated result
bool operationCompleted = false;
public MainWindow()
{
InitializeComponent();
}
private void btn_Zero_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "0";
this.textBox.Text += input;
if(operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_One_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "1";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Two_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "2";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Three_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "3";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Four_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "4";
this.textBox.Text += input;
if (operationCompleted)
{
input = string.Empty;
textBox.Text = string.Empty;
operand1 = string.Empty;
operand2 = string.Empty;
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Five_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "5";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Six_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "6";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Seven_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "7";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Eight_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "8";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Nine_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += "9";
this.textBox.Text += input;
if (operationCompleted)
{
btn_Clear_Click(sender, e);
operationCompleted = false;
}
btn_Equals.Focus();
}
private void btn_Dot_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
input += ".";
this.textBox.Text += input;
btn_Equals.Focus();
}
private void btn_Minus_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '-';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Plus_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '+';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Multiply_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '*';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
private void btn_Divide_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation += '/';
input = string.Empty;
textBox.Text = string.Empty;
btn_Equals.Focus();
}
// The equals works for the first sum but not for any after it
private void btn_Equals_Click(object sender, RoutedEventArgs e)
{
{
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
if (operation == '+')
{
result = num1 + num2;
textBox.Text = result.ToString();
}
else if (operation == '-')
{
result = num1 - num2;
textBox.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
textBox.Text = result.ToString();
}
else if (operation == '/')
{
if (num1 != 0 || num2 != 0)
{
result = num1 / num2;
textBox.Text = result.ToString();
}
else
{
textBox.Text = "Cannot divide by zero";
}
}
operationCompleted = true;
this.Focus();
}
}
private void btn_Clear_Click(object sender, RoutedEventArgs e)
{
this.textBox.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
this.result = 0.0;
operationCompleted = false;
}
}
You need to clear the operation. You have operation += '/'. So every time someone clicks one of the operation buttons you are appending that operation to the string. When you click the equals button, you do not have a catch for bad operations so nothing happens.
Suggestion:
Clear the operation on finish.
Add a default else if to account for bad operations so that you can handle the error.
when I press 9 and then press times the display shows 9 and when I press 9 again it'll add it to the first number (9) instead of clearing the first 9. so it thinks its 9*99. how do I clear the display on the first input after an operation?
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Form1()
{
InitializeComponent();
}
private void Multiply_Click(object sender, EventArgs e)
{
operation = '*';
if (operand1 != "")
{
calculate();
}
else
{
operand1 = input;
}
}
private void Minus_Click(object sender, EventArgs e)
{
operation = '-';
if (operand1 != "")
{
calculate();
}
else
{
operand1 = input;
}
}
private void Addition_Click(object sender, EventArgs e)
{
operation = '+';
if (operand1 != "")
{
calculate();
}
else
{
operand1 = input;
}
}
private void Divide_Click(object sender, EventArgs e)
{
operation = '/';
if (operand1 != "")
{
calculate();
}
else
{
operand1 = input;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void five_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "5";
this.textBox1.Text += input;
}
private void Zero_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "0";
this.textBox1.Text += input;
}
private void one_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "1";
this.textBox1.Text += input;
}
private void two_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "2";
this.textBox1.Text += input;
}
private void three_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "3";
this.textBox1.Text += input;
}
private void four_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "4";
this.textBox1.Text += input;
}
private void six_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "6";
this.textBox1.Text += input;
}
private void seven_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "7";
this.textBox1.Text += input;
}
private void eight_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "8";
this.textBox1.Text += input;
}
private void nine_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
private void Equal_Click(object sender, EventArgs e)
{
calculate();
}
private void Decimal_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
input += ".";
this.textBox1.Text += input;
}
private void Clear_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
public void calculate()
{
Console.WriteLine("performing operation" + operand1 + " " + operation + " " + operand2 + "= ???" + "the input is?" + input);
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
if (operation == '*')
{
result = num1 * num2;
}
if (operation == '-')
{
result = num1 * num2;
}
if (operation == '+')
{
result = num1 * num2;
}
if (operation == '/')
{
result = num1 * num2;
}
textBox1.Text = result.ToString();
input = result.ToString();
}
}
}
Add the following to your class Form1:
bool operationExcecuted = false;
And in your calculate method add the following line:
operationExcecuted = true;
And in every number input check if an operation is excecuted. If so clear the input and add a new number. For example the number 9:
private void nine_Click(object sender, EventArgs e)
{
if(operationExcecuted)
{
input = String.Empty;
operationExcecuted = false;
}
this.textBox1.Text = "";
input += "9";
this.textBox1.Text += input;
}
I think, that's because of you adding
input += "9"
this.textBox1.text += input;
You're always adding the 9 to the variable input. Try cleaning input. Before adding a new number.
I have two questions on a program I am creating that is a calculator (I know, wow, so amazing). I am trying to give it a Sin button but anytime I try to use it, it will not allow me to use it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
/*This is an app that will be used for carrying out claculations
entered by the user. I am attemtpting to include keyboard shortcuts
also.*/
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
/* ------------Declaration of needed variables to carry out mathematical functions------------ */
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
/* ------------MainWindow, not altered------------ */
public MainWindow()
{
InitializeComponent();
}
/* ------------Number and number customization buttons------------ */
private void Decimal_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += ".";
this.ViewerDisplay.Text += input;
}
private void Zero_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "0";
this.ViewerDisplay.Text += input;
}
private void One_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "1";
this.ViewerDisplay.Text += input;
}
private void Two_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "2";
this.ViewerDisplay.Text += input;
}
private void Three_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "3";
this.ViewerDisplay.Text += input;
}
private void Four_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "4";
this.ViewerDisplay.Text += input;
}
private void Five_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "5";
this.ViewerDisplay.Text += input;
}
private void Six_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "6";
this.ViewerDisplay.Text += input;
}
private void Seven_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "7";
this.ViewerDisplay.Text += input;
}
private void Eight_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "8";
this.ViewerDisplay.Text += input;
}
private void Nine_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "";
input += "9";
this.ViewerDisplay.Text += input;
}
/* ------------Operation buttons------------*/
private void Minus_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation = '-';
input = string.Empty;
}
private void Plus_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation = '+';
input = string.Empty;
}
private void Divide_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation = '/';
input = string.Empty;
}
private void Multiply_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation = '*';
input = string.Empty;
}
private void Equal_Click(object sender, RoutedEventArgs e)
{
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
if (operation == '+')
{
result = num1 + num2;
ViewerDisplay.Text = result.ToString();
}
if (operation == '-')
{
result = num1 - num2;
ViewerDisplay.Text = result.ToString();
}
if (operation == '*')
{
result = num1 * num2;
ViewerDisplay.Text = result.ToString();
}
if (operation == '/')
{
if (num2 != 0)
{
result = num1 / num2;
ViewerDisplay.Text = result.ToString();
}
else
{
ViewerDisplay.Text = "Undefined";
}
}
if (operation == 'S')
{
result = Math.Sin(num1);
ViewerDisplay.Text = result.ToString();
}
}
private void Clear_Click(object sender, RoutedEventArgs e)
{
this.ViewerDisplay.Text = "0.0";
input = string.Empty;
operand1 = string.Empty;
operand2 = string.Empty;
}
private void Fnc_Click(object sender, RoutedEventArgs e)
{
this.MainGrid.Visibility = Visibility.Collapsed;
this.SecondaryGrid.Visibility = Visibility.Visible;
}
private void Sin_Click(object sender, RoutedEventArgs e)
{
operand1 = input;
operation = 'S';
input = string.Empty;
input = "Sin(";
}
private void Cos_Click(object sender, RoutedEventArgs e)
{
}
private void Tan_Click(object sender, RoutedEventArgs e)
{
}
I also would like to know how to design different characters for the button such as a square root emblem.
The problem is after pressing the sin button, it adds "Sin(" to the input, and then TryParse fails because the input has letters in it. Also, you are calling Math.Sin on num1, which is the number entered before you press the sin button.
Just a note, Math.Sin returns the result in radians, if you want it in degress, use:
Math.Sin((num2*Math.PI)/180)
Where num2 is the last number entered in the box, after "Sin(". I've tested this, I made a crappy calculator real quick to do so.
Now, about designing your different characters like the square root symbol. First off, the square root symbol (√) can be put into a button, label, etc, by setting it's Text property (Content for WPF) to "\u221A". Like this:
myButton.Content = "\u221A"; //WPF
myButton.Text = "\u221A"; //WinForms
For other symbols, try searching online, if you can't find any, make a picture and set the button's background to that image.
I cannot for the life of me figure out how to continuously show the input and then output of the numbers for this.
What do you do to store the numbers somewhere and then show them? I've gone in so many circles that I have confused myself to oblivion.
I know what needs to be done but not how or exactly where to do it?
public partial class Form1 : Form
{
char c;
double num1;
double num2;
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
txtBox.Text += 0;
}
private void btn1_Click(object sender, EventArgs e)
{
txtBox.Text += 1;
}
private void btn2_Click(object sender, EventArgs e)
{
txtBox.Text += 2;
}
private void btn3_Click(object sender, EventArgs e)
{
txtBox.Text += 3;
}
private void btn4_Click(object sender, EventArgs e)
{
txtBox.Text += 4;
}
private void btn5_Click(object sender, EventArgs e)
{
txtBox.Text += 5;
}
private void btn6_Click(object sender, EventArgs e)
{
txtBox.Text += 6;
}
private void btn7_Click(object sender, EventArgs e)
{
txtBox.Text += 7;
}
private void btn8_Click(object sender, EventArgs e)
{
txtBox.Text += 8;
}
private void btn9_Click(object sender, EventArgs e)
{
txtBox.Text += 9;
}
private void btnDecimal_Click(object sender, EventArgs e)
{
if (!txtBox.Text.Contains('.'))
txtBox.Text += '.';
}
private void btnAddition_Click(object sender, EventArgs e)
{
c = '+';
num1 = double.Parse(txtBox.Text);
txtBox.Text = string.Empty;
}
private void btnSubtraction_Click(object sender, EventArgs e)
{
c = '-';
num1 = double.Parse(txtBox.Text);
txtBox.Text = string.Empty;
}
private void btnMultiplication_Click(object sender, EventArgs e)
{
c = '*';
num1 = double.Parse(txtBox.Text);
txtBox.Text = string.Empty;
}
private void btnDivision_Click(object sender, EventArgs e)
{
c = '/';
num1 = double.Parse(txtBox.Text);
txtBox.Text = string.Empty;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtBox.Clear();
}
private void btnEqual_Click(object sender, EventArgs e)
{
num2 = double.Parse(txtBox.Text);
double result;
switch (c)
{
case '+':
result = num1 + num2;
txtBox.Text = result.ToString();
break;
case '-':
result = num1 - num2;
txtBox.Text = result.ToString();
break;
case '/':
if (num2 != 0)
{
result = num1 / num2;
txtBox.Text = result.ToString();
}
else
{
txtBox.Text = "You can't divide by zero... sign up for Math 100 please =)";
}
break;
case '*':
result = num1 * num2;
txtBox.Text = result.ToString();
break;
}
}
}
}
I'll let you wire this up, but here is what I threw together.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private string op;
private string num1;
private string num2;
private void handleNumberButtonClick(object sender, System.EventArgs e)
{
var num = Convert.ToInt16(((Button)sender).Tag);
if (op == null)
num1 += num;
else
num2 += num;
PrintEquation(num1, op, num2);
}
private void PrintEquation(string first, string oper = null, string second = null, string equals = null, string result = null)
{
txtBox.Text = first + oper + second + equals + result;
}
private void handleOperatorButtonClick(object sender, System.EventArgs e)
{
op = ((Button)sender).Tag.ToString();
PrintEquation(num1, op);
}
private void btnDecimal_Click(object sender, EventArgs e)
{
if (op == null && !num1.Contains(".")) num1 += ".";
if (op != null && !num2.Contains(".")) num2 += ".";
this.PrintEquation(num1, op, num2);
}
private void btnClear_Click(object sender, EventArgs e)
{
txtBox.Clear();
op = null;
num1 = null;
num2 = null;
}
private void btnEqual_Click(object sender, EventArgs e)
{
double result = 0;
var first = Convert.ToDouble(num1);
var second = Convert.ToDouble(num2);
switch (op)
{
case "+":
result = first + second;
break;
case "-":
result = first - second;
break;
case "/":
if (second != 0)
{
result = first / second;
}
else
{
errorLbl.Text = "You can't divide by zero... sign up for Math 100 please =)";
}
break;
case "*":
result = first * second;
break;
}
this.PrintEquation(num1, op, num2, "=", result.ToString());
}
}
To give you a clue on how things go together, this is what the button should look like in the .designer.cs file:
this.btn0.Location = new System.Drawing.Point(12, 60);
this.btn0.Name = "btn0";
this.btn0.Size = new System.Drawing.Size(75, 23);
this.btn0.TabIndex = 1;
this.btn0.Tag = "0";
this.btn0.Text = "btn0";
this.btn0.UseVisualStyleBackColor = true;
this.btn0.Click += new System.EventHandler(this.handleNumberButtonClick);
Notice the .Tag value and the eventhandler.
The Operators should look like this(this is add, notice the tag and the Click Event handler):
this.addButton.Location = new System.Drawing.Point(178, 60);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(75, 23);
this.addButton.TabIndex = 11;
this.addButton.Tag = "+";
this.addButton.Text = "add";
this.addButton.UseVisualStyleBackColor = true;
this.addButton.Click += new System.EventHandler(this.handleOperatorButtonClick);
And the decimal, like this:
this.button4.Location = new System.Drawing.Point(178, 176);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 15;
this.button4.Tag = ".";
this.button4.Text = "decimal";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.btnDecimal_Click);
Oh, and throw this in your clear button definition
this.button5.Click += new System.EventHandler(this.btnClear_Click);
Special thanks to #Mark Hall and #Martin James for their insights