12,454,477 members (56,411 online)
Rate this:
See more:
Hello Code project

Im currently making a calculator, and after my last question was successful i have a new question.

How do i make it how the whole calculation, like this= 5 + 5 = 10, 5 * 5 = 25 and so on?
when i try to make i show it, with this command:
Before:
```private void button10_Click(object sender, EventArgs e) {
if (textBox1.Text == "")
{
return;
}
else
{
plus = false;
minus = false;
multiply = false;
divide = true;
equal = false;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";```

After:
```private void button10_Click(object sender, EventArgs e) {
if (textBox1.Text == "")
{
return;
}
else
{
plus = false;
minus = false;
multiply = false;
divide = true;
equal = false;
textBox1.Tag = textBox1.Text;
textBox1.Text = "/"```
When i try this, it will crash. it's the same with all of them ( +, -, *, /)

And i want to put in Square root, how can i do that?

Sorry if my english is bad.
PS some of it is in Danish so just ignore the comments after //
Thanks for the help

```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 Calculator{
public partial class CalForm : Form {
bool plus = false;
bool minus = false;
bool multiply = false;
bool divide = false;
bool equal = false;
public CalForm() {
InitializeComponent();
Text = "Calculator";
}

private void CalForm_Load(object sender, EventArgs e) {
}
// 1
private void button1_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "1";
}
// 2
private void button2_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "2";
}
// 3
private void button3_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "3";
}
// 4
private void button4_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "4";
}
// 5
private void button5_Click(object sender, EventArgs e) {
if (equal)
{
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "5";
}
// 6
private void button6_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "6";
}
// 7
private void button7_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "7";
}
// 8
private void button8_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "8";
}
// 9
private void button9_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "9";
}
// 0
private void button14_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "0";
}
// ,
private void button15_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
if (textBox1.Text.Contains(",")) {
return;
}
else {
textBox1.Text = textBox1.Text + ",";
}
}
//Dette er plus
private void button11_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
}
else
{
plus = true;
minus = false;
multiply = false;
divide = false;
equal = false;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";
}
}
//Dette er "lig med" for regne reglerne i lommeregneren
private void button13_Click(object sender, EventArgs e) {
equal = true;
if (plus) {
decimal deo = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
textBox1.Text = deo.ToString();
}
if (minus) {
decimal deo = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
textBox1.Text = deo.ToString();
}
if (multiply) {
decimal deo = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
textBox1.Text = deo.ToString();
}
if (divide) {
//Man kan ikke dividere med 0
try {
decimal deo = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
textBox1.Text = deo.ToString();
}
catch (DivideByZeroException) {
MessageBox.Show("Man kan ikke divider med 0");
}
}
}
//Dette er minus
private void button12_Click(object sender, EventArgs e) {
if (textBox1.Text == "")
{
return;
}
else
{
plus = false;
minus = true;
multiply = false;
divide = false;
equal = false;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";
}
}
//Dette er gange
private void button16_Click(object sender, EventArgs e) {
if (textBox1.Text == "")
{
return;
}
else
{
plus = false;
minus = false;
multiply = true;
divide = false;
equal = false;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";
}
}
//Dette er clear
private void button17_Click(object sender, EventArgs e) {
plus = minus = multiply = divide = false;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";
}
// Dette er divider
private void button10_Click(object sender, EventArgs e) {
if (textBox1.Text == "")
{
return;
}
else
{
plus = false;
minus = false;
multiply = false;
divide = true;
equal = false;
textBox1.Tag = textBox1.Text;
textBox1.Text = "/";
}
}
//Dette er pi
private void button19_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "3,14";
}
//Dette er tau
private void button21_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "6,28";
}
//Dette er e
private void button20_Click(object sender, EventArgs e) {
if (equal) {
textBox1.Text = "";
equal = false;
}
textBox1.Text = textBox1.Text + "2,71";

}
}
}```
Posted 11-Nov-12 2:58am
psychic6000 11-Nov-12 9:13am

too much operation code plus/minus etc, use char
ch='+' and switch case etc

Rate this:

## Solution 1

The way you have done it so far is ok, for a simple +-*/ calculator, (although it's getting a bit stretched already, as you have probably noticed) but to start enhancing it significantly, you need to look at changing the way you do things.

The problem is that when you start to introduce squareroot, you are starting with operators that take a single operand, rather than the two you are currently expecting, and this can make your code start to "creak" a bit...

Is there any reason why you are doing this as a "normal" or "old fashioned" calculator, with buttons and a numeric only display? You have a full keyboard, and text you can do anything with...

Why not look at the task differently: instead of having a "value" textbox, why not have an "expression" text box instead?
So if the user types 1+2 you automatically evaluate it and produce a result of 3, which goes into a "result" textbox (probably read only). This means producing a parser (which looks at the string and breaks it into the operands and operators) and then evaluating the results, but there are a lot of Google articles which will help you with that, or there is an example here: A Very Simple Parser[^]

If nothing else, it will give you a different look at the problem!
Rate this:

## Solution 3

'//Hi there,I just wanna show you my example of calculator.I made it few years ago.Maybe it will be helpful for youSee image: http://s19.postimage.org/nsjhbkjv7/calc.png[^]

```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MojKalkulator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int clear = 1; decimal result, mresult = 0; string op;
int Clear(int cl)
{
switch (cl)
{
case 1:
{
label1.Text = "";
} break;
case 2:
{
label1.Text = ""; label2.Text = ""; op = "";
} break;
case 3:
{
label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('r'));
} break;
case 4:
{
label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.IndexOf('s'));
} break;
case 5:
{
label1.Text = ""; label2.Text = label2.Text.Remove(label2.Text.LastIndexOf(' ') + 1);
} break;
}

return 0;
}
decimal eval(string op)
{
clear = 1;
try
{
switch (op)
{
case "+": result = result + Convert.ToDecimal(label1.Text); break;
case "-": result = result - Convert.ToDecimal(label1.Text); break;
case "*": result = result * Convert.ToDecimal(label1.Text); break;
case "/": result = result / Convert.ToDecimal(label1.Text); break;
case "Mod": result = result % Convert.ToDecimal(label1.Text); break;
default: result = Convert.ToDecimal(label1.Text); break;
}
}
catch (System.OverflowException) { label2.Text = ""; label2.Text = "Overflow"; clear = 2; }
catch (System.DivideByZeroException) { label2.Text = ""; label2.Text = "Cannot divide by zero"; clear = 2; }
return result;
}

private void button30_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "0";
else System.Media.SystemSounds.Beep.Play();
}

private void button25_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "1";
else System.Media.SystemSounds.Beep.Play();

}

private void button24_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "2";
else System.Media.SystemSounds.Beep.Play();

}

private void button23_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "3";
else System.Media.SystemSounds.Beep.Play();

}

private void button20_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "4";
else System.Media.SystemSounds.Beep.Play();

}

private void button19_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "5";
else System.Media.SystemSounds.Beep.Play();

}

private void button18_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "6";
else System.Media.SystemSounds.Beep.Play();

}

private void button15_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "7";
else System.Media.SystemSounds.Beep.Play();
}

private void button14_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "8";
else System.Media.SystemSounds.Beep.Play();

}

private void button13_Click(object sender, EventArgs e)
{
clear = Clear(clear);
if (label1.Text.Length < 28)
label1.Text = label1.Text + "9";
else System.Media.SystemSounds.Beep.Play();

}

private void button28_Click(object sender, EventArgs e)
{
if (label1.Text.Contains("."))
{
System.Media.SystemSounds.Beep.Play();
}
else
label1.Text = label1.Text + " . ";
}
private void button27_Click(object sender, EventArgs e)
{
if (clear == 3 || clear == 4 || clear == 5)
label2.Text = label2.Text + " + ";
else label2.Text = label2.Text + label1.Text + " + ";
label1.Text = eval(op).ToString();
op = "+";
}

private void button22_Click(object sender, EventArgs e)
{
if (clear == 3 || clear == 4 || clear == 5)
label2.Text = label2.Text + " - ";
else label2.Text = label2.Text + label1.Text + " - ";
label1.Text = eval(op).ToString();
op = "-";
}

private void button17_Click(object sender, EventArgs e)
{
if (clear == 3 || clear == 4 || clear == 5)
label2.Text = label2.Text + " / ";
else label2.Text = label2.Text + label1.Text + "/";
label1.Text = eval(op).ToString();
op = "/";
}

private void button12_Click(object sender, EventArgs e)
{
if (clear == 3 || clear == 4 || clear == 5)
label2.Text = label2.Text + " * ";
else label2.Text = label2.Text + label1.Text + " * ";
label1.Text = eval(op).ToString();
op = "*";
}

private void button10_Click(object sender, EventArgs e)
{
if (clear == 3 || clear == 4 || clear == 5)
label2.Text = label2.Text + " Mod ";
else label2.Text = label2.Text + label1.Text + " Mod ";
label1.Text = eval(op).ToString();
op = "Mod";
}

private void button7_Click(object sender, EventArgs e)
{
if (label2.Text.Contains("reciproc"))
{
label2.Text = label2.Text.Insert(label2.Text.IndexOf('r'), "reciproc("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
}
else label2.Text = label2.Text + "reciproc(" + label1.Text + ")";
label1.Text = (1 / Convert.ToDecimal(label1.Text)).ToString(); clear = 3;
}

private void button11_Click(object sender, EventArgs e)
{
if (label2.Text.Contains("sqrt"))
{
label2.Text = label2.Text.Insert(label2.Text.IndexOf('s'), "sqrt("); label2.Text = label2.Text.Insert(label2.Text.IndexOf(')'), ")");
}
else label2.Text = label2.Text + "sqrt(" + label1.Text + ")";
label1.Text = Math.Sqrt(Convert.ToDouble(label1.Text)).ToString(); clear = 4;
}

private void button16_Click(object sender, EventArgs e)
{
label1.Text = (result * Convert.ToDecimal(label1.Text) / 100).ToString();
label2.Text = label2.Text + label1.Text; clear = 5;

}

private void button6_Click(object sender, EventArgs e)
{
if (label1.Text.Contains("-"))
{
label1.Text = label1.Text.Remove(label1.Text.IndexOf('-'), 1);
}
else label1.Text = "-" + label1.Text;

}

private void button26_Click(object sender, EventArgs e)
{
if (clear != 3 && clear != 4 && clear != 5)
label2.Text = label2.Text + label1.Text;
label1.Text = eval(op).ToString(); clear = 2;

}

private void button8_Click(object sender, EventArgs e)
{
Clear(1); clear = 1; label1.Text = "0";
}

private void button9_Click(object sender, EventArgs e)
{
Clear(2); clear = 1; label1.Text = "0"; result = 0;
}

private void button3_Click(object sender, EventArgs e)
{
mresult = Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
}

private void button2_Click(object sender, EventArgs e)
{
label1.Text = mresult.ToString(); clear = 1;
}

private void button4_Click(object sender, EventArgs e)
{
mresult = mresult + Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
}

private void button5_Click(object sender, EventArgs e)
{
mresult = mresult - Convert.ToDecimal(label1.Text); clear = 1; label3.Text = "M";
}

private void button1_Click(object sender, EventArgs e)
{
mresult = 0; clear = 1; label3.Text = "";
}

private void label1_TextChanged(object sender, EventArgs e)
{
if (label1.Text.Length <= 20)
{
Font font = new Font("Consolas", 14, FontStyle.Regular); label1.Font = font;
}
else if (label1.Text.Length > 20 && label1.Text.Length <= 26)
{
Font font = new Font("Consolas", 11, FontStyle.Regular); label1.Font = font;
}
else { Font font = new Font("Consolas", 9, FontStyle.Regular); label1.Font = font; }
}

}
}
```

Top Experts
Last 24hrsThis month
 OriginalGriff 290 Maciej Los 285 Richard MacCutchan 108 Member 12658776 90 phil.o 89
 OriginalGriff 6,373 ppolymorphe 3,878 Karthik Bangalore 3,640 Maciej Los 3,640 Richard Deeming 2,835