Professional Documents
Culture Documents
Basic Calculator in C# - C# Tutorials - Dream
Basic Calculator in C# - C# Tutorials - Dream
Code
1 of 17
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
Last
Basic Calculator in C#
PsychoCoder
General Discussion
Caffeine Lounge
Corner Cubicle
Student Campus
POPULAR
In this tutoral, Basic Calculator in C#, we will look at
creating a basic calculator. The calculator will have the
following functionality:
Addition
Subtraction
Division
Multiplication
Square Root
Exponents (Power Of)
Clear Entry
Clear All
There will be a 2nd tutorial that will cover some more
advanced features such as
Adding a number to memory
Removing a number from memory
Calculating with a number in memory
Entering numbers by typing
The first thing you need to do is create a new project in
Visual Studio (or Visual Basic Express Edition if thats what
you use). Once you have created your new project you need
to create your user interface, your user interface should
look like this:
Software Development
Industry News
Introduce Yourself
Give It A Try!
Nightmare.In.Code
Programming Help
C and C++
VB6
Java
VB.NET
C#
ASP.NET
.NET Framework
PHP
Ruby
Subtraction
Division
Multiplication
Exponents (x^)
Inverse (1/x)
Square Root (sqrt)
Decimal
Equals
Backspace
CE (Clear Entry)
C (Clear All)
ReadOnly TextBox for input (Make sure TabStop is also set to
False)
How you setup your user interface is up to you, but
remember people are used to a calculator looking a certain
way so you may wish to follow my example. In this tutorial I
will show you how to code two of the number buttons (since
all 10 are the same except the zero button), how to code
the calculations buttons, the clear buttons and the
backspace buttons. For two of the buttons we will need to
use built-in math functions in the .Net Framework:
System.Math.Sqrt Method (http://msdn2.microsoft.com
/en-us/library/system.math.sqrt.aspx) Used to calculate
Square Roots
Python
C# Tutorials
C# Learning Series Properties
C# Learning Series Classes & Objects
Detect Partially
ColdFusion
Databases
Other Languages
Game Development
Mobile Development
52 Weeks Of Code
Corrupted Image
Breadcrumb Navigation
in .NET
Parameterizing your
SQL Queries: The
Web Development
Web Development
HTML & CSS
JavaScript
database.
Graphic Design
12/3/2012 11:00 PM
2 of 17
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
Signature Maker
Tutorial (Build an
Blogging
Advertising
| Terms of Use |SEO
Privacy
Policy |
Application)
& Advertising
About Us
NotePad (RexPad)
Web Servers & Hosting
Now, in our calculator we need some Global variables to
hold different items and states in our calculator, such as
Tutorial: Step by Step
Site Check
which calculation are we performing, does the input area
already have a decimal, whether we can enter values into
Slide
Show
Desktop Reserved
the input area and
to hold values2001-2012
while we perform
Copyright
MediaGroup1
LLC,
All& Rights
calculations. Add the following code to the top of your code,
Background
Tutorial
LLC Production
- Version
6.0.2.1.36
A MediaGroup1
this is the global variables
we need in our calculator:
Server: secure3
What does this error
01
02
03
04
05
06
07
08
09
10
mean? Understanding
the common errors.
Tic Tac Toe Step by Step
219 More C# Tutorials...
Reference Sheets
C# Snippets
Read value from
Resource file in C#
Get connection string
from the web.config
Kill a process if its
running
Kill a process if it's
running
Check if a process is
running
Search Text file for
String
Validate IP address with
Regular Expression
Retrieve populated
12/3/2012 11:00 PM
3 of 17
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
DIC Chatroom
Monthly Drawing
Find us on Facebook
Dream.In.Code
Like
25,258 people like Dream.In.Code.
Sumon
Hakan
Andrea
Much
Jj
jean
Hesham
Ouadie
Abdo
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
12/3/2012 11:00 PM
4 of 17
22
23
24
25
26
27
28
29
30
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
else
{
//Since the length isnt > 1
//make the text 0.
txtInput.Text = "0.";
}
}
}
}
12/3/2012 11:00 PM
5 of 17
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
12/3/2012 11:00 PM
6 of 17
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
12/3/2012 11:00 PM
7 of 17
12
13
14
15
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
11
12
13
14
15
16
17
18
19
20
21
22
12/3/2012 11:00 PM
8 of 17
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
Those are the buttons you need for a Basic calculator. The
final thing we're going to look at is the procedure that
actually does the calculations, CalculateTotals. Here the
first thing we do is set our variable valHolder2 to the
current value of the input area.
We then do a switch(calcFunc) on the value of calcFunc so
we know which calculations to perform. We perform our
calculations (add, subtract, divide, multiply, exponent, etc)
and set the results to the input area so the user can see
their results. Finally we set the inputEntry flag to False.
This is what this procedure looks like:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
12/3/2012 11:00 PM
9 of 17
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
System.Math.Pow(valHolder1, valHolder2);
break;
}
//set our input area to the value of the
calculation
30
txtInput.Text = valHolder1.ToString();
31
inputStatus = false;
32 }
27
28
29
(http://optimized-by.rubiconproject.com/t/9358/16370
/39994-15.3315924.3370992?url=http%3A%2F
%2Fadoptuskids.org)
Replies To: Basic Calculator in C#
Allen.Brumley
(http://www.dreamincode.net/forums/index.php?app=forums&
module=forums§ion=findpost&pid=254209) PsychoCoder, on 9
Sep, 2007 - 08:36 PM, said:
In this tutoral, Basic Calculator in C#, we will look at
12/3/2012 11:00 PM
10 of 17
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
PsychoCoder
MeghaRazdan
(http://www.dreamincode.net/forums/index.php?app=forums&
module=forums§ion=findpost&pid=300870) Allen.Brumley, on
25 Jan, 2008 - 08:50 AM, said:
(http://www.dreamincode.net/forums/index.php?app=forums&
module=forums§ion=findpost&pid=254209) PsychoCoder, on 9
Sep, 2007 - 08:36 PM, said:
In this tutoral, Basic Calculator in C#, we will look at
creating a basic calculator. The calculator will have the
following functionality:
The Equals button is quite simple. Here, we first check to
make sure our input area has a value and that our
valHolder1 variable isn't a zero (Divide by 0 is a bad thing).
If both of these are true we call the CalculateTotals
procedure to perform our calculations based on the value of
the calcFunc flag. We then clear the value of calcFunc and
toggle the hasDecimal flag to False. This is done like this:
01
02
03
04
05
06
12/3/2012 11:00 PM
11 of 17
07
08
09
10
11
12
13
14
15
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
input box
tmpValue =
System.Double.Parse(txtInput.Text);
//Perform the square root
tmpValue = System.Math.Sqrt(tmpValue);
//Display the results in the input box
txtInput.Text = tmpValue.ToString();
//Clear the decimal flag
hasDecimal = false;
}
}
PsychoCoder
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
12/3/2012 11:00 PM
12 of 17
28
29
30
31
32
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
}
//set our input area to the value of the
calculation
txtInput.Text = valHolder1.ToString();
inputStatus = false;
}
mandy2010
thaaanx!!
WorkingC#
(http://www.dreamincode.net/forums/index.php?app=forums&
module=forums§ion=findpost&pid=302879) PsychoCoder, on
31 Jan, 2008 - 08:49 AM, said:
This is the code for the equals button, which is listed at the
end of the tutorial:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Nomantheone
Elliander
12/3/2012 11:00 PM
13 of 17
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
The Tutorial was overall very helpful, but there were a few
problems with the code.
As it is, it actually allowed the user to enter two decimal
points. This was fixed easily by adding another:
hasDecimal = true;
at the very end of the decimal code.
It also had a Fatal Error. If the user enters a number, then
doesn't enter a second number, it crashes on pressing "="
because there is no value stored valHolder2. It would run
better if by default it had a value of 0 like windows
calculator uses so if the user doesn't enter a second number
it can't crash anything.
I ran the program beside windows calculator to see the
difference. One major difference was quickly seen.
For example, in adding 1 + 3 = 4, in windows Calculator, if
you press "=" again, it would then say "7" then "10" then
"13" etc. But in this program it would say "8" then "16" then
"32" then "64" etc.
In other words, Windows Calculator would take the value of
the second number entered and use that each time "=" is
pressed but your code acts as a doubler. I wouldn't mind
seeing how it would work to make it run like Windows
Calculator.
Edit:
Another serious problem with this code is that powerof
doesn't really work at all. If the answer would be less than
0, it displays nothing, and if the answer would be greater
than 0, it displays 0.
It seems to be impossible to do 999 times .001 times .001
because of the way the decimal points are written. when I
try, I get 999 times .001 times 1. If I try to press "=" after
it and try again, same thing. So certain forms of long math
are pretty much impossible as it is written.
The code is also unable to display more than 15 characters.
Which can be a real problem. Sometimes it cuts off the end
of so many digits, other time it does this:
9999999991 times 9999999991 = 99999999820000000081
, in windows calculator, but = 9.999999982E+19 in your
code. (exactly 15 characters) even though the display area
and textbox settings should allow for more. The average
user won't know how to read that.
This post has been edited by Elliander: 15 May 2009 - 07:23 PM
source144
Irish18
01
02
03
04
05
06
07
08
09
10
11
12
13
14
12/3/2012 11:00 PM
14 of 17
15
16
17
18
19
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
txtInput.Text = cmd1.Text;
//Toggle inputStatus to True
inputStatus = true;
}
}
papuccino1
Irish18
(http://www.dreamincode.net/forums/index.php?app=forums&
module=forums§ion=findpost&pid=674951) papuccino1, on 15
Jun, 2009 - 03:15 PM, said:
Irish18, you probably DO NOT have a TextBox called
"txtInput" on your form. Triple-check for that. I'm 999%
percent sure that's the problem.
Gutted, i knew id made some stupid rookie mistake. :(
Thanks!
Irish18
(http://www.dreamincode.net/forums/index.php?app=forums&
module=forums§ion=findpost&pid=674951) papuccino1, on 15
Jun, 2009 - 03:15 PM, said:
Irish18, you probably DO NOT have a TextBox called
"txtInput" on your form. Triple-check for that. I'm 999%
percent sure that's the problem.
Gyargh, I found the name of my TextBox, it was called
'textBox1'; so i put that infront of '.Text' and its giving the
same error message
This is what i have so far;
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
12/3/2012 11:00 PM
15 of 17
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
lesPaul456
(http://www.dreamincode.net/forums/index.php?app=forums&
module=forums§ion=findpost&pid=675685) Irish18, on 16 Jun,
2009 - 08:40 AM, said:
(http://www.dreamincode.net/forums/index.php?app=forums&
module=forums§ion=findpost&pid=674951) papuccino1, on 15
Jun, 2009 - 03:15 PM, said:
Irish18, you probably DO NOT have a TextBox called
"txtInput" on your form. Triple-check for that. I'm 999%
percent sure that's the problem.
Gyargh, I found the name of my TextBox, it was called
'textBox1'; so i put that infront of '.Text' and its giving the
same error message
This is what i have so far;
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
12/3/2012 11:00 PM
16 of 17
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
In the code you pasted, the button1 click event looks like
it's in the "Program" class. If that's right, then there's your
problem. This method should be in the "Form1" class.
(4 Pages)
1
2
3
Last
Related
C#
beta
Topics
Error
Message Help
On Basic
Calculator
Code - Last
Snippet Of
Code Is
Giving Me An
Error And I
Don't
Understand
Creating
Calculator
With C#
How Can
Calculator In
The C#
12/3/2012 11:00 PM
17 of 17
http://www.dreamincode.net/forums/topic/32968-basic-calculator-in-c#/
C# Class Help
How To Make
An Simple
Calculator In
C#
Calculator Store Value
Mortgage
Calculator In
C#
Basic
Calculator Implementing
Keyboard
Input
Basic
Calculator
Textbox
Calculator
12/3/2012 11:00 PM