• (2 Pages)
  • +
  • 1
  • 2

Create a calculator Rate Topic: ***** 1 Votes

#1 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Posted 30 April 2008 - 08:50 AM

Here I will shoe you how to create a simple calculator that can add, subtract, divide, multiply and change sign state. Most of the code is commented so it is easy to learn.

For this you will need 19 buttons and one text box (txtNUMBER). Have a look at the picture that is at the bottom on how I have made mine.

first we will declare the variables:
'Declare the global variables to be used throughout the form
Dim mfirst As Single
Dim msecond As Single
Dim manswer As Single
' Declare the global variables for the operators: Add,Sub,Mul and DIV
Dim mbutton As Integer
'Change the sign of the number from + or - or vice versa
' Depending on its state now they show in txtNUMBER text box
Dim Signstate As Boolean



Second we will make it so that the number buttons actually do something so add the code into the your form:
Private Sub cmd0_Click()
'Put the value 0 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "0"
End Sub

Private Sub cmd1_Click()
'Put the value 1 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "1"
End Sub

Private Sub cmd2_Click()
'Put the value 2 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "2"
End Sub

Private Sub cmd3_Click()
'Put the value 3 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "3"
End Sub

Private Sub cmd4_Click()
'Put the value 4 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "4"
End Sub

Private Sub cmd5_Click()
'Put the value 5 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "5"
End Sub

Private Sub cmd6_Click()
'Put the value 6 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "6"
End Sub

Private Sub cmd7_Click()
'Put the value 7 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "7"
End Sub

Private Sub cmd8_Click()
'Put the value 8 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "8"
End Sub

Private Sub cmd9_Click()
'Put the value 9 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "9"
End Sub



Noticing that all the number buttons are called cmd# (# is the number) as your buttons need to have these names.

Now we will make the math buttons. The first is the add button:

Private Sub cmdADD_Click()
'User slected the add button
mbutton = 1
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub



Subtract:
Private Sub cmdSUBTRACT_Click()
'User slected the minus button
mbutton = 2
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub



Multiply:
Private Sub cmdMULTIPLY_Click()
'User slected the multiply button
mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub



Divide:
Private Sub cmdDIVIDE_Click()
'User slected the Divide button
mbutton = 4
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub



Now we will make the equals button work otherwise theres no point to all this.
Private Sub cmdEQUALS_Click()
msecond = Val(txtNUMBER)

Select Case mbutton
Case Is = 1
manswer = mfirst + msecond
Case Is = 2
manswer = mfirst - msecond
Case Is = 3
manswer = mfirst * msecond
Case Is = 4
manswer = mfirst / msecond
End Select
txtNUMBER = manswer
End Sub



Now you can test your calculator to see it work. Once you have test we have just a few more bits to add such as the decimal point:
Private Sub cmdDOT_Click()
txtNUMBER = txtNUMBER + "."
End Sub



The next bit of code is to change the sign state:
Private Sub cmdSIGN_Click()
'Sign state = false on load of form
If txtNUMBER = "-" + txtNUMBER Then
	MsgBox "error start again"
End If
If Signstate = False Then
txtNUMBER = "-" + txtNUMBER
Signstate = True
Else
'SignState = True

minusvalue = Val(txtNUMBER)
'Value now positive
minusvalue = Val("-1" * minusvalue)
txtNUMBER = minusvalue
Signstate = False

End If
End Sub



The next two bits are to cancel meaning empty the textbox and to end the program.
Private Sub cmdEXIT_Click()
Unload frmCALCULATOR
End Sub

Private Sub cmdCANCEL_Click()
'Remove the values in the txtNUMBER text box
txtNUMBER = " "
End Sub



Now your calculator is complete all you have to do is test it. Since this has a lot of code I have added a text file with the code in it. If you do have any problems then just comment me and I will do what I can to help.

Attached File(s)

  • Attached File  code.txt (3.27K)
    Number of downloads: 18478


Is This A Good Question/Topic? 0
  • +

Replies To: Create a calculator

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Posted 07 May 2008 - 10:37 AM

I would suggest it may be prudent to implement some error checking on your divide case - division by zero is never desirable.
Was This Post Helpful? 0
  • +
  • -

#3 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Posted 07 May 2008 - 11:24 AM

Will update it now so that it does an error check on it.
Was This Post Helpful? 0
  • +
  • -

#4 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Posted 07 May 2008 - 11:42 AM

Put this:
	If Val(mfirst) > 0 And Val(msecond) > 0 Then
		manswer = Val(mfirst) / Val(msecond)
		
	ElseIf Val(Text1.Text) = 0 And Val(msecond) > 0 Then
		manswer = Val(mfirst) / Val(msecond)
		
	ElseIf Val(Text1.Text) > 0 And Val(msecond) = 0 Then
		manswer = Val(mfirst) / Val(msecond)
		
	ElseIf Val(Text1.Text) = 0 And Val(msecond) = 0 Then
		manswer = Val(mfirst) / Val(msecond + 1)
		
	End If



After:

Select Case mbutton
Case Is = 1
manswer = mfirst + msecond
Case Is = 2
manswer = mfirst - msecond
Case Is = 3
manswer = mfirst * msecond
Case Is = 4
manswer = mfirst / msecond
End Select



As this is an error check for if your dividing 0 by 0.
Was This Post Helpful? 0
  • +
  • -

#5 tthompson1114  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-June 08

Posted 22 June 2008 - 06:32 PM

When I am writing the code for each button to add the number i press to the text box it gives me an error that says "Operator '+' is not defined for types 'System.Windows.Forms.TextBox' and 'String'. I'm pretty new to Visual Basic 2008 and I have no clue what to do. Any suggestions to this problem?
Was This Post Helpful? 0
  • +
  • -

#6 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Posted 22 June 2008 - 07:29 PM

This is Visual Basic 6 not 2008 sorry
Was This Post Helpful? 0
  • +
  • -

#7 kEsiah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-June 08

Posted 26 June 2008 - 01:26 AM

hmm hi der! can i ask u a question? this codes is for standard calculator?

can you help me to my project? my project is to make a standard calculator that has a clear,backspace,and 0.?
Was This Post Helpful? 0
  • +
  • -

#8 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Posted 26 June 2008 - 07:12 AM

It does have a "0" but yes I can help you if you send me what you already have then I will see what I can do.
Was This Post Helpful? 0
  • +
  • -

#9 Reverand Dave  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 375
  • Joined: 27-July 08

Posted 14 August 2008 - 09:46 PM

View Posttthompson1114, on 22 Jun, 2008 - 06:32 PM, said:

When I am writing the code for each button to add the number i press to the text box it gives me an error that says "Operator '+' is not defined for types 'System.Windows.Forms.TextBox' and 'String'. I'm pretty new to Visual Basic 2008 and I have no clue what to do. Any suggestions to this problem?


Instead of using the "+" you could use the "&" operator. That way it will include the text in the text box and the int from the button press. so instead of
Private Sub cmd1_Click()
'Put the value 1 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "1"
End Sub



you would have
Private Sub cmd1_Click()
'Put the value 1 into the txtNUMBER text box
txtNUMBER = txtNUMBER & "1"
End Sub


This post has been edited by Reverand Dave: 14 August 2008 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

#10 halogod32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-September 08

Posted 18 September 2008 - 02:46 PM

If Any One Is Interested I Remade The Whole Code In VB2008 or VB.NET As Its Become Known. I Will Post It Below. It Works Perfectly And I Used All The Same Names As Sam For The Buttons And Variables, cmd0, cmd1 etc.
Public Class Form1
	'Declare the global variables to be used throughout the form
	Dim mfirst As Single
	Dim msecond As Single
	Dim manswer As Single
	' Declare the global variables for the operators: Add,Sub,Mul and DIV
	Dim mbutton As Integer
	'Change the sign of the number from + or - or vice versa
	' Depending on its state now they show in txtNUMBER text box
	Dim Signstate As Boolean
	Private Sub cmdCLEAR_Click()
		'Remove the values in the txtNUMBER text box
	End Sub
	Private Sub cmdEXIT_Click()
		Me.Close()
	End Sub

	Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "0"
	End Sub

	Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "1"
	End Sub

	Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "2"
	End Sub

	Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "3"
	End Sub

	Private Sub cmd4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "4"
	End Sub

	Private Sub cmd5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd5.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "5"
	End Sub

	Private Sub cmd6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd6.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "6"
	End Sub

	Private Sub cmd7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd7.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "7"
	End Sub

	Private Sub cmd8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd8.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "8"
	End Sub

	Private Sub cmd9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd9.Click
		'Put the value 0 into the txtNUMBER text box
		txtNUMBER.Text = txtNUMBER.Text & "9"
	End Sub

	Private Sub cmdSIGN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSIGN.Click
		Dim MINUSVALUE
		'Sign state = false on load of form
		If txtNUMBER.Text = "-" & txtNUMBER.Text Then
			MsgBox("error start again")
		End If
		If Signstate = False Then
			txtNUMBER.Text = "-" & txtNUMBER.Text
			Signstate = True
		Else
			'SignState = True

			MINUSVALUE = Val(txtNUMBER.Text)
			'Value now positive
			MINUSVALUE = Val("-1" * MINUSVALUE)
			txtNUMBER.Text = MINUSVALUE
			Signstate = False
		End If
	End Sub

	Private Sub cmdSUBTRACT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSUBTRACT.Click
		'User slected the minus button
		mbutton = 2
		'Convert into a number and transfer the value from
		'The text box on the form into the first number
		mfirst = Val(txtNUMBER.Text)

		txtNUMBER.Text = ""
	End Sub

	Private Sub cmdDOT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDOT.Click
		txtNUMBER.Text = txtNUMBER.Text & "."
	End Sub

	Private Sub cmdADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdADD.Click
		'User slected the add button
		mbutton = 1
		'Convert into a number and transfer the value from
		'The text box on the form into the first number
		mfirst = Val(txtNUMBER.Text)

		txtNUMBER.Text = ""
	End Sub

	Private Sub cmdDIVIDE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDIVIDE.Click
		'User slected the Divide button
		mbutton = 4
		'Convert into a number and transfer the value from
		'The text box on the form into the first number
		mfirst = Val(txtNUMBER.Text)

		txtNUMBER.Text = ""
	End Sub

	Private Sub cmdEQUALS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEQUALS.Click
		msecond = Val(txtNUMBER.Text)

		Select Case mbutton
			Case Is = 1
				manswer = mfirst + msecond
			Case Is = 2
				manswer = mfirst - msecond
			Case Is = 3
				manswer = mfirst * msecond
			Case Is = 4
				manswer = mfirst / msecond
		End Select
		txtNUMBER.Text = manswer
	End Sub

	Private Sub cmdMULTIPLY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMULTIPLY.Click
		'User slected the multiply button
		mbutton = 3
		'Convert into a number and transfer the value from
		'The text box on the form into the first number
		mfirst = Val(txtNUMBER.Text)

		txtNUMBER.Text = ""
	End Sub

	Private Sub cmdCLEAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCLEAR.Click
		mbutton = 0
		txtNUMBER.Text = ""
		manswer = Nothing
	End Sub

	Private Sub cmdCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCE.Click
		txtNUMBER.Text = ""
	End Sub
End Class


This post has been edited by halogod32: 18 September 2008 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#11 halogod32  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-September 08

Posted 18 September 2008 - 02:54 PM

If You Want A Sample Email/IM Me At
Gmail: {REMOVED}
Yahoo: {REMOVED}
xfire: {REMOVED}
Was This Post Helpful? 0
  • +
  • -

#12 akhileshbc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 179
  • Joined: 27-September 08

Posted 27 September 2008 - 10:07 PM

Hi, why can't you create a control array for this..???
Create a button and give its name as cmd, then copy it and paste it. It will ask you, whether you want to create a control array. Click Yes. Continue pasting till you reached 10 buttons. Then do the below coding:
Private Sub cmd_Click(Index As Integer)
txtNUMBER = txtNUMBER & CStr(Index)
End Sub

Instead of the long code:
Private Sub cmd0_Click()
'Put the value 0 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "0"
End Sub

Private Sub cmd1_Click()
'Put the value 1 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "1"
End Sub

Private Sub cmd2_Click()
'Put the value 2 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "2"
End Sub

Private Sub cmd3_Click()
'Put the value 3 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "3"
End Sub

Private Sub cmd4_Click()
'Put the value 4 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "4"
End Sub

Private Sub cmd5_Click()
'Put the value 5 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "5"
End Sub

Private Sub cmd6_Click()
'Put the value 6 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "6"
End Sub

Private Sub cmd7_Click()
'Put the value 7 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "7"
End Sub

Private Sub cmd8_Click()
'Put the value 8 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "8"
End Sub

Private Sub cmd9_Click()
'Put the value 9 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "9"
End Sub


It will be easy to use that single line instead of that 20 or 30 lines...:)
Was This Post Helpful? 0
  • +
  • -

#13 Quin  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 19
  • View blog
  • Posts: 383
  • Joined: 16-October 08

Posted 16 October 2008 - 03:00 AM

Is there a way to have it so that when you enter a number after clicking the equals button, txtNUMBER is cleared and then the new number appears, instead of it being added to the end of the answer?

Also...

View Postsam_benne, on 7 May, 2008 - 11:42 AM, said:

Put this:
	If Val(mfirst) > 0 And Val(msecond) > 0 Then
		manswer = Val(mfirst) / Val(msecond)
		
	ElseIf Val(Text1.Text) = 0 And Val(msecond) > 0 Then
		manswer = Val(mfirst) / Val(msecond)
		
	ElseIf Val(Text1.Text) > 0 And Val(msecond) = 0 Then
		manswer = Val(mfirst) / Val(msecond)
		
	ElseIf Val(Text1.Text) = 0 And Val(msecond) = 0 Then
		manswer = Val(mfirst) / Val(msecond + 1)
		
	End If



After:

Select Case mbutton
Case Is = 1
manswer = mfirst + msecond
Case Is = 2
manswer = mfirst - msecond
Case Is = 3
manswer = mfirst * msecond
Case Is = 4
manswer = mfirst / msecond
End Select



As this is an error check for if your dividing 0 by 0.



I keep getting a daft little message. When the second number is 0, then "Run time error '11': Division by zero"
And, when it is 0 divided by 0 then, "Run time error '6': Overflow"

Both, when clicking on Debug, it highlights the code "manswer = mfirst / msecond" in Case 4.

This post has been edited by Quin: 16 October 2008 - 03:24 AM

Was This Post Helpful? 0
  • +
  • -

#14 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Posted 22 March 2009 - 03:02 AM

What about if the equation was 9999999999999999999999 x 999999999999999999 . How would the calculator cope with this equation? Wouldnt it be an Overflow error?
Was This Post Helpful? 0
  • +
  • -

#15 alfalah88  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 29-May 09

Posted 29 May 2009 - 09:19 PM

View Postsam_benne, on 30 Apr, 2008 - 07:50 AM, said:

Here I will shoe you how to create a simple calculator that can add, subtract, divide, multiply and change sign state. Most of the code is commented so it is easy to learn.

For this you will need 19 buttons and one text box (txtNUMBER). Have a look at the picture that is at the bottom on how I have made mine.

first we will declare the variables:
'Declare the global variables to be used throughout the form
Dim mfirst As Single
Dim msecond As Single
Dim manswer As Single
' Declare the global variables for the operators: Add,Sub,Mul and DIV
Dim mbutton As Integer
'Change the sign of the number from + or - or vice versa
' Depending on its state now they show in txtNUMBER text box
Dim Signstate As Boolean



Second we will make it so that the number buttons actually do something so add the code into the your form:
Private Sub cmd0_Click()
'Put the value 0 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "0"
End Sub

Private Sub cmd1_Click()
'Put the value 1 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "1"
End Sub

Private Sub cmd2_Click()
'Put the value 2 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "2"
End Sub

Private Sub cmd3_Click()
'Put the value 3 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "3"
End Sub

Private Sub cmd4_Click()
'Put the value 4 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "4"
End Sub

Private Sub cmd5_Click()
'Put the value 5 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "5"
End Sub

Private Sub cmd6_Click()
'Put the value 6 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "6"
End Sub

Private Sub cmd7_Click()
'Put the value 7 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "7"
End Sub

Private Sub cmd8_Click()
'Put the value 8 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "8"
End Sub

Private Sub cmd9_Click()
'Put the value 9 into the txtNUMBER text box
txtNUMBER = txtNUMBER + "9"
End Sub



Noticing that all the number buttons are called cmd# (# is the number) as your buttons need to have these names.

Now we will make the math buttons. The first is the add button:

Private Sub cmdADD_Click()
'User slected the add button
mbutton = 1
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub



Subtract:
Private Sub cmdSUBTRACT_Click()
'User slected the minus button
mbutton = 2
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub



Multiply:
Private Sub cmdMULTIPLY_Click()
'User slected the multiply button
mbutton = 3
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub



Divide:
Private Sub cmdDIVIDE_Click()
'User slected the Divide button
mbutton = 4
'Convert into a number and transfer the value from
'The text box on the form into the first number
mfirst = Val(txtNUMBER)

txtNUMBER = ""
End Sub



Now we will make the equals button work otherwise theres no point to all this.
Private Sub cmdEQUALS_Click()
msecond = Val(txtNUMBER)

Select Case mbutton
Case Is = 1
manswer = mfirst + msecond
Case Is = 2
manswer = mfirst - msecond
Case Is = 3
manswer = mfirst * msecond
Case Is = 4
manswer = mfirst / msecond
End Select
txtNUMBER = manswer
End Sub



Now you can test your calculator to see it work. Once you have test we have just a few more bits to add such as the decimal point:
Private Sub cmdDOT_Click()
txtNUMBER = txtNUMBER + "."
End Sub



The next bit of code is to change the sign state:
Private Sub cmdSIGN_Click()
'Sign state = false on load of form
If txtNUMBER = "-" + txtNUMBER Then
	MsgBox "error start again"
End If
If Signstate = False Then
txtNUMBER = "-" + txtNUMBER
Signstate = True
Else
'SignState = True

minusvalue = Val(txtNUMBER)
'Value now positive
minusvalue = Val("-1" * minusvalue)
txtNUMBER = minusvalue
Signstate = False

End If
End Sub



The next two bits are to cancel meaning empty the textbox and to end the program.
Private Sub cmdEXIT_Click()
Unload frmCALCULATOR
End Sub

Private Sub cmdCANCEL_Click()
'Remove the values in the txtNUMBER text box
txtNUMBER = " "
End Sub



Now your calculator is complete all you have to do is test it. Since this has a lot of code I have added a text file with the code in it. If you do have any problems then just comment me and I will do what I can to help.

Attachment attachment






hello.i have complete this code,but it still not work.when i click the all cmd button especially the number,it doesn't show at the text box.why?the only button work are cancel,and exit.is there any problem?i also have checked my declaration but still does not work.please help me..
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2