PDA

View Full Version : [C#] basic calculator [tutorial]



Skilled4u
July 9th, 2010, 12:14
I. Stuff needed:

-Visual studio ( using vs10 pro in this tut but 8 works too)
thats about it.

II. Tutorial:

1- designing calculator:

well im not gonna show u how its done because its pretty easy... u just add buttons and number them.

My design: Only the registered members can see the link.

2- adding the codes to the buttons:

-allright so double click on the first button wich is 1

ok Now Vs will automaticly add this line into the code:

private void button1_Click(object sender, EventArgs e)
{

}
it doesnt have to be button1_click it just depends on when u added the button.

then add this code inside the brackets: ill explain after the code...

textBox1.Text = TextBox1.Text + "1";

allright its pretty simple all it does is add the 1 to the textbox not much to explain here.

all right do the same to the buttons 0,1,2,3,4,5,6,7,8,9,

just do the same thing u did for the 1 to the other by replacing the 1 by its number.

- Now its time for the decimal wich is the .

ok so first double click the decimal button
VS will add the 3 lines of codes by itself
between the brackets add this:


if (textBox1.Text.contains("."))
{
return;
}


well its simple it means if the textbox already contains the decimal "."

then it executes whats in between the brackets and wich is return; wich will make it skip over the code or w.e u get the point.

and if it doesnt it will execute this code by using the else:


else
{
textBox1.Text = textBox1.Text + ".";
}

i already explained what it means.

allright were done with the decimal.

Complete code for decimal:

if (textBox1.Text.contains("."))
{
return;
}
else
{
textBox1.Text = textBox1.Text + ".";
}


- allright its time for the +/-

double click it and add this code between the brackets:

if (textBox1.Text.contains("-"))
{

textBox1.Text = textBox1.Text.Remove(0, 1);
}
else
{
textBox1.Text = "-" + textBox1.Text;
}


when pressed it makes negative number positive and positive ones negative.

- now its time for the plus

allright now double click the plus and do ctrl+f and find : public Form1()

and add this:
bool plus = false; before "public Form1()" so it looks like this:

bool plus = false;
public Form1()

allright now add this code between the brackets of the click button of the plus:


if (textBox1.Text == "")
{
return;
}
else
{
plus = true;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";
}
}

- now for the equal

allright double click it and between its brackets add this code:

if (plus)
{
decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString();
}

wich means if the plus boolean is true
then add the tag from before and the new one in the textbox together and then show it in the txtbox.

- now for the minus

do the exact same things u did for the plus exept change the plus boolean to minus.
and do the exact same thing for division and multiplication

and name the booleans:

divide
minus
multiply

and i repeat follow the same procedure from the plus.

so ull have:

minus:

if (textBox1.Text == "")
{
return;
}
else
{
minus = true;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";
}
multiplication:

if (textBox1.Text == "")
{
return;
}
else
{
multiply = true;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";
}
division:

if (textBox1.Text == "")
{
return;
}
else
{
divide = true;
textBox1.Tag = textBox1.Text;
textBox1.Text = "";
}
and dont forget to add the booleans at the beginning.

- allright now lets do the clear button

allright double click the clear button
now between this brackets add this code:

plus = minus = divide = multiply = false;
textBox1.Text = "";
textBox1.Tag = "";

allright this will clear out the textbox and the tag well its pretty easy.
before testing it its time to make the equal for the minus, divide, and multiply...

- Adding equal to all the shit :P

its very simple...
between the brackets of the equals button copy the plus paragraph...

w/e im jsut gonna show u the codes:

multiply:
if (multiply)
{
decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString();
}
divide:
if (divide)
{
decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString();
}
minus:
if (minus)
{
decimal dec = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString();
}

theres nothing to explain here since i did it before

now u can test out the clear.

-Now lets fix that stupid bug #1

the bug is... when u lets say add 2+2 and then click = u get 4 and then u click 2 u 42
try it out...

k lets fix it:

k let me explain what well be doing...
well be adding a new boolean... wich is equal so when u click a number it checks if equal is true if it is it clears out the textbox ...

ok 1: create a new bool under the others : equal = false;
2: right after the first braquets of the minus button code : equal = true;
well it means when u click the equal it turns the boolto true...

allright now were gonna create a new method...

type this in the button1 code:

checkifequal();

now right click what u just wrote and click create method stub

now this will be written:
private void checkifequal()
{
}

there will be something btween the brackets erase it and copy this:

if (equal)
{
textBox1.Text = "";
equal = false;
}
and now type checkifequal();

in every number code and the decimal one only.

so before it executes the number's code it jumps to this method and etc...

Thats it ;)

comment :)

my pker pure
July 10th, 2010, 11:19
Visual Basic I'm guessing?

Skilled4u
July 10th, 2010, 11:47
no visual studio 10 C#

Mish
July 10th, 2010, 12:56
Great tut! helped me alot thanks i'm getting into C# now, it's a bit like Java I think and the GUI for C# is great so thumbsup, I would rep if I could.

Skilled4u
July 10th, 2010, 12:57
thanks u english tard :p haha jk ull pay later.

Mish
July 10th, 2010, 13:06
thanks u english tard :p haha jk ull pay later.

What did I do? all I did was compliment you
rofl

Skilled4u
July 10th, 2010, 13:06
haha good job editing ur post at the last min :p

Mish
July 10th, 2010, 13:09
haha good job editing ur post at the last min :p

lolimafunnybunny

Skilled4u
July 10th, 2010, 13:11
... never say that again. i beg u

reese
July 20th, 2010, 03:20
Lmfao what could he of possibly said?
@Topic
Goodjob. Repp++ if I could

Skilled4u
July 20th, 2010, 10:49
thanks reese :)

iiNarbz
July 20th, 2010, 18:21
This is nice. But im trying to make a advanced calculator this really helped alot thank's Repp+++

Skilled4u
July 20th, 2010, 18:43
haha thanks :p ill try editing this into advanced if im bored sometimes.

James
July 21st, 2010, 01:21
I tried this but im not good with Java. Only graphics and RSPS coding.

Skilled4u
July 21st, 2010, 07:15
first rsps is java.
and second this isnt java ;)