C# calculations visual studios - c#

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.

Related

Simple Calculator in C# Forms with Running Total

I am studying online for a generalized degree and we are tasked with developing a simple calculator using .Forms
I was able to create the GUI quite simply, but am struggling with the development in coding a variable that tracks the running total, and allows the user to execute more than one operation.
Currently, my code allows for two inputs and 1 operation. Once totaled, if the user selects another number to operate, the code simply drops the first input and relpaces it with the second original input, then takes the third input and replaces the second input for the new calculation.
for example, if I wanted to add 3 + 6, my calculator would give me 9, but if I then press + 4, instead of getting 13 (3+6+4, or better yet, 9+4) I get 10 (6+4)
I'm hoping someone can point me in the correct direction to create a variable for a running total that tracks the total after an operation is executed and then allows the user to continue performing operations without having to clear the results of only two inputs.
Thanks
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 IT232M2_McCarver_Par1
{
public partial class Calculator : Form
{
string input = string.Empty;
string operand1 = string.Empty;
string operand2 = string.Empty;
char operation;
double result = 0.0;
public Calculator()
{
InitializeComponent();
}
private void button11_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 6;
this.lblDisplay.Text += input;
}
private void button10_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 1;
this.lblDisplay.Text += input;
}
private void cmdZero_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 0;
this.lblDisplay.Text += input;
}
private void cmdTwo_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 2;
this.lblDisplay.Text += input;
}
private void cmdThree_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 3;
this.lblDisplay.Text += input;
}
private void cmdFour_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 4;
this.lblDisplay.Text += input;
}
private void cmdFive_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 5;
this.lblDisplay.Text += input;
}
private void cmdSeven_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 7;
this.lblDisplay.Text += input;
}
private void cmdEight_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 8;
this.lblDisplay.Text += input;
}
private void cmdNine_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 9;
this.lblDisplay.Text += input;
}
private void cmdClear_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
this.input = string.Empty;
this.operand1 = string.Empty;
this.operand2 = string.Empty;
}
private void cmdAdd_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '+';
input = string.Empty;
}
private void cmdSubtract_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '-';
input = string.Empty;
}
private void cmdMultiply_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '*';
input = string.Empty;
}
private void cmdDivide_Click(object sender, EventArgs e)
{
operand1 = input;
operation = '/';
input = string.Empty;
}
private void cmdEqual_Click(object sender, EventArgs e)
{
string runningTotal = result.ToString();
operand2 = input;
double num1, num2;
double.TryParse(operand1, out num1);
double.TryParse(operand2, out num2);
if (operation == '+')
{
result = num1 + num2;
lblDisplay.Text = result.ToString();
}
else if (operation == '-')
{
result = num1 - num2;
lblDisplay.Text = result.ToString();
}
else if (operation == '*')
{
result = num1 * num2;
lblDisplay.Text = result.ToString();
}
else if (operation == '/')
{
if (num2 != 0)
{
result = num1 / num2;
lblDisplay.Text = result.ToString();
}
else
{
lblDisplay.Text = "Cant /Zero";
}
}
}
}
}
As this is an assignment I would expect your code to contain a lot more comments, detailing your algorithm
I think you need to think about how a real calculator works. I assume you're making a simple one where any operation can trigger a calculate if both operands are set - after the calculation if you put the result in operand1 and unset operand2:
Imagine the user presses 1+2+4=
Set operand1 to 1
Set operation to +
Set operand2 to 2
Both operands are set so calculate, setting operand1 to 4 and unsetting operand2 and operator
Set operation to +
Calculate because = is pressed but don't unset operand2 (because the user can hammer = and add 4 each time)
Consider having a Calculate method that takes a Boolean indicating if it should unset operand2/operator or not. Calculate should always transfer the result of the op into operand1. Whether the operator is set or not should be used to determine if it's operand1 or operand2 that the user is typing. Personally I would do away with input and concatenate operand1 and 2 directly. I'd make the decision as to which to extend in the click handler. I'd have a dedicated variable for when equals had been recently pressed that will unset everything if the user presses a number button after pressing equals, or keeps operand1 and sets a new operation and insets operand2 if it's an operation that the user presses after equals
There are other things we could clean up in your code. This fragment had me puzzled for a while:
private void cmdEight_Click(object sender, EventArgs e)
{
this.lblDisplay.Text = "";
input += 8;
this.lblDisplay.Text += input;
}
This would be more logical:
private void cmdEight_Click(object sender, EventArgs e)
{
input += 8;
lblDisplay.Text = input:
}
But really, if your button just has 8 as its text you can do this:
private void cmdAny_Click(object sender, EventArgs e)
{
input += (sender as Button).Text;
lblDisplay.Text = input:
}
And every button uses this same handler
If you're making a calculator that understands multiply and divide are done before add and subtract you're going to have to store all the operands and operators in a list and then go through the list doing certain operations first upon pressing equals

How to fix the code for Subtaction, multiplication and division C#

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 .

Simple Calculator Woes(Can't get the equals button to work more than once)

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.

C# Calculator Logic

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.

Using Keyboard as and Input Device on C# Calculator

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

Categories