Jump to content


Photo
- - - - -

Simple calculator


  • Please log in to reply
2 replies to this topic

#1 MicroByte

MicroByte

    Young Padawan

  • Members
  • Pip
  • 19 posts

Posted 21 December 2005 - 01:23 PM

This tutorial shows how to make a timer based calculator with +, - , / and * functions.

Now, File/New Project and make Windows Application.
Then make enough space to create 16 buttons and 2 text fields. And make sure that Button1 is in number 1's place, Button2 is in number 2's place and so on. Numbers 1-9(for some reason, 0 doesnt seem to work [or desimals]). Change the text of the buttons, Button1 to 1, Button2 to 2... but don't change name, or you have to change the code below.
Then insert then buttons anyway you like and the rest of the buttons, you can rename anyway you want. I renamed them in Finnish, laske; jaettuna....
Make 1 textbox, rename it text1, clear the text1 area, then again make textbox2, rename it to text2, clear the text2 area and just write the following code to buttons:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Text1.Text = Text1.Text + "1"
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Text1.Text = Text1.Text + "2"
	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		Text1.Text = Text1.Text + "3"
	End Sub

	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		Text1.Text = Text1.Text + "4"
	End Sub

	Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
		Text1.Text = Text1.Text + "5"
	End Sub

	Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
		Text1.Text = Text1.Text + "6"
	End Sub

	Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
		Text1.Text = Text1.Text + "7"
	End Sub

	Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
		Text1.Text = Text1.Text + "8"
	End Sub

	Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
		Text1.Text = Text1.Text + "9"
	End Sub

	Private Sub zero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		Text1.Text = Text1.Text + "0"
	End Sub
Ok, that makes the numbers see on the textbox. Now you need to have 5 buttons for calculating actions. -, +, / and * buttons, with name you prefer. I call them plus (+), miinus(-), jaettuna(/) and kertaa(*). Then insert 4 timers there on form, anywhere. Check the name is Timer1, Timer2, Timer3, and Timer4. After that, write this code to each of the calculating function buttons:
Private Sub miinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miinus.Click
		Text2.Text = Text1.Text
		Text1.Text = ""
		Timer1.Enabled = True
		Timer2.Enabled = False
		Timer3.Enabled = False
		Timer4.Enabled = False
	End Sub
	Private Sub plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles plus.Click
		Text2.Text = Text1.Text
		Text1.Text = ""
		Timer1.Enabled = False
		Timer2.Enabled = True
		Timer3.Enabled = False
		Timer4.Enabled = False
	End Sub
	Private Sub kertaa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kertaa.Click
		Text2.Text = Text1.Text
		Text1.Text = ""
		Timer1.Enabled = False
		Timer2.Enabled = False
		Timer3.Enabled = True
		Timer4.Enabled = False
	End Sub
	Private Sub jaettuna_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles jaettuna.Click
		Text2.Text = Text1.Text
		Text1.Text = ""
		Timer1.Enabled = False
		Timer2.Enabled = False
		Timer3.Enabled = False
		Timer4.Enabled = True
	End Sub

That makes the timer recognize the order. Then this code to "="(I call it laske) button:
Private Sub laske_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles laske.Click
		If Timer1.Enabled = True Then
			Text1.Text = Val(Text2.Text) - Val(Text1.Text)
		ElseIf Timer2.Enabled = True Then
			Text1.Text = Val(Text2.Text) + Val(Text1.Text)
		ElseIf Timer3.Enabled = True Then
			Text1.Text = Val(Text2.Text) * Val(Text1.Text)
		ElseIf Timer4.Enabled = True Then
			Text1.Text = Val(Text2.Text) / Val(Text1.Text)
		End If
	End Sub
That makes the calculate process. Then add these lines to up:
Dim lasku As Integer
Now, all we need is the "Clear" button, it's simpler than going to shop:
Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click
		Text1.Text = ""
		Text2.Text = ""
	End Sub
All done!

--------------------------------------------------------
IMPORTANT: Feel free to change text or the name of the buttons anyway you like. Just remember, when you change button name, you must change it in the code section too.
This is very simple counter, i dont expect it to be good or anything, but this is the first thing i made for 6 hours thinking :D

--------------------------------------------------------

My result: http://iltsu.homelin...obit/Laskin.zip

Edited by Donna, 08 January 2006 - 07:28 PM.


#2 ronson

ronson

    Banned For Being Purple

  • Twodded Staff
  • PipPip
  • 480 posts
  • Gender:Male
  • Location:North Yorkshire, UK
  • Interests:Rugby, Programming, Building Computers, Networking, Web Design and...........BINGE DRINKING!!!!!!.

Posted 08 January 2006 - 07:16 PM

Ok i've found quite a few problems with your tutorial.

1) The naming of the controls, they MUST be renamed. Not only for standards and good practice, but if your making a big program giving them a useful name does good with the prefix of btn, tmr, lbl, txt, lst, cbo etc...

2) The code you have given is very confusing but not just for me but for anyone, there are no comments or explanations of the code so for any new programmers they might find it VERY confusing/intimidating and dismiss the tutorial.

3) You should include screen shots with the tutorial because i personally find just text tutorial borings but screen shots also help the user understand more etc.

4) Your tutorial also doesn't handle decimal numbers

5) The coding is very messy and it could be done easier.

Hope this helps and don't take this negative, it's only advice.

James

P.S. PM me with any questions.

#3 _*Raremandan_*

_*Raremandan_*
  • Guests

Posted 10 January 2006 - 02:15 PM

I found this.

I'm currently learning by doing tutorials, and too understand that i'd have too copy and paste the code, in which i'm not doing as I hate learning by C+P so i'm writing the code down, only he leaves no explanation too what it all does -_-

- Dan

Ok i've found quite a few problems with your tutorial.

1) The naming of the controls, they MUST be renamed. Not only for standards and good practice, but if your making a big program giving them a useful name does good with the prefix of btn, tmr, lbl, txt, lst, cbo etc...

2) The code you have given is very confusing but not just for me but for anyone, there are no comments or explanations of the code so for any new programmers they might find it VERY confusing/intimidating and dismiss the tutorial.

3) You should include screen shots with the tutorial because i personally find just text tutorial borings but screen shots also help the user understand more etc.

4) Your tutorial also doesn't handle decimal numbers

5) The coding is very messy and it could be done easier.

Hope this helps and don't take this negative, it's only advice.

James

P.S. PM me with any questions.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users