Today, I will teach you how to create a calculator using C#.Net. This tutorial contains a step by step procedure that you need to follow orderly with ease. This is a simple calculator with functions that has been done accurately.
To start with:
Open Microsoft Visual Studio 2008 and create new Windows Form Application for C#. Then do the following design of a Form just like this.
After that, go to the Solution Explorer, double click the “View Code” to display the code editor.
In the code editor, declare all the variables that are needed.
1 2 |
double tot = 0; string LogicalOperator; |
do the following codes for function of the buttons (0-9 and .) .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
private void btn1_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn1.Text; } else { txtView.Text += btn1.Text; } } private void btn2_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn2.Text; } else { txtView.Text += btn2.Text; } } private void btn3_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn3.Text; } else { txtView.Text += btn3.Text; } } private void btn0_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn0.Text; } else { txtView.Text += btn0.Text; } } private void btn8_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn8.Text; } else { txtView.Text += btn8.Text; } } private void btn7_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn7.Text; } else { txtView.Text += btn7.Text; } } private void btn9_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn9.Text; } else { txtView.Text += btn9.Text; } } private void btn6_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn6.Text; } else { txtView.Text += btn6.Text; } } private void btn5_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn5.Text; } else { txtView.Text += btn5.Text; } } private void btn4_Click(object sender, EventArgs e) { if (txtView.Text == "0") { txtView.Clear(); txtView.Text += btn4.Text; } else { txtView.Text += btn4.Text; } } private void btnDote_Click(object sender, EventArgs e) { if (!txtView.Text.Contains(".")) { txtView.Text += btnDote.Text; } } |
After that, do the following codes for function of the MDAS(x,/,+,-).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
private void btnPlus_Click(object sender, EventArgs e) { tot += double.Parse(txtView.Text); LogicalOperator = "plus"; txtView.Clear(); } private void btnminus_Click(object sender, EventArgs e) { tot += double.Parse(txtView.Text); LogicalOperator = "minus"; txtView.Clear(); } private void btnMultiply_Click(object sender, EventArgs e) { tot += double.Parse(txtView.Text); LogicalOperator = "multiply"; txtView.Clear(); } private void btnDivide_Click(object sender, EventArgs e) { tot += double.Parse(txtView.Text); LogicalOperator = "divide"; txtView.Clear(); } |
Do the following codes for the “Clear” button.
1 2 3 4 5 6 |
private void btnClear_Click(object sender, EventArgs e) { txtView.Text = "0"; tot = 0; } |
Finally, do the following codes for the function of the equals(=) sign.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
private void btnEqual_Click(object sender, EventArgs e) { switch (LogicalOperator){ case "plus": tot = tot + double.Parse(txtView.Text); txtView.Text = tot.ToString(); tot = 0; LogicalOperator = ""; break ; case "minus": tot = tot - double.Parse(txtView.Text); txtView.Text = tot.ToString(); tot = 0; LogicalOperator = ""; break; case "multiply": tot = tot * double.Parse(txtView.Text); txtView.Text = tot.ToString(); tot = 0; LogicalOperator = ""; break; case "divide": tot = tot / double.Parse(txtView.Text); txtView.Text = tot.ToString(); tot = 0; LogicalOperator = ""; break; default : txtView.Text = tot.ToString(); break; } |
For all students who need programmer for your thesis system or anyone who needs a sourcecode in any programming languages. You can contact me @ :
Email – [email protected]
Mobile No. – 09305235027 – tnt