• (2 Pages)
  • +
  • 1
  • 2

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

#16 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

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

Posted 30 May 2009 - 02:20 AM

Have you made sure that you have named everything correctly.
Was This Post Helpful? 0
  • +
  • -

#17 olig1905  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-August 09

Posted 09 August 2009 - 10:21 AM

thanx for that.. i needed to get this done before tomorrow as a bit of vb practise which i was set and needed it as a little reminder. However i am using VB2008 express ( i used to use VB 6.0 but have been advised to move to 2008 for what i am to be doing for the next 2 weeks)

i have an issue with it, the only number button that does anything is 1 and it puts a stupid number in the txtNUMBER

Note: I have used slightly different variables and names.

Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "1"

	End Sub
	Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "2"

	End Sub
	Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "3"

	End Sub
	Private Sub cmd4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "4"

	End Sub
	Private Sub cmd5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "5"

	End Sub
	Private Sub cmd6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "6"

	End Sub
	Private Sub cmd7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "7"

	End Sub
	Private Sub cmd8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "8"

	End Sub
	Private Sub cmd9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "9"

	End Sub
	Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click

		txtdisplay.Text = txtdisplay.Text + "0"



i think its menna say txtdisplay.text & "1" unless & and + mean same thing

the random number is 0987654321
Was This Post Helpful? 0
  • +
  • -

#18 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

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

Posted 10 August 2009 - 01:21 AM

Ask in the appropriate forum, in this case, VB.NET.
Was This Post Helpful? 0
  • +
  • -

#19 Pokebub0825  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 11-August 09

Posted 14 August 2009 - 11:09 PM

THX For the help
Was This Post Helpful? 0
  • +
  • -

#20 kali1613  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-October 09

Posted 09 October 2009 - 03:15 AM

i have done all and understand all the commands and codes for all the buttons but when i press the equal sign it doesn't show the answer? why is that? im using VB6... and i named all my buttons right... help pls
Was This Post Helpful? 0
  • +
  • -

#21 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

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

Posted 11 October 2009 - 04:43 AM

Hi,

Could you post that question in the programming forum: http://www.dreaminco...showforum21.htm

This is where everyone will tend to look to answer questions.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#22 poly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-October 09

Posted 31 October 2009 - 03:07 AM

View Postkali1613, on 9 Oct, 2009 - 02:15 AM, said:

i have done all and understand all the commands and codes for all the buttons but when i press the equal sign it doesn't show the answer? why is that? im using VB6... and i named all my buttons right... help pls


may be you did not assign the value calculated to the display variable
try this
txtNumber = manswer this goes on the equal sign button

View Postkali1613, on 9 Oct, 2009 - 02:15 AM, said:

i have done all and understand all the commands and codes for all the buttons but when i press the equal sign it doesn't show the answer? why is that? im using VB6... and i named all my buttons right... help pls


may be you did not assign the value calculated to the display variable
try this
txtNumber = manswer
this goes on the equal sign button
Was This Post Helpful? 0
  • +
  • -

#23 SusanPogue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-May 10

Posted 26 May 2010 - 09:39 AM

I have noticed that the decimal will let you add more than one decimal. How do you account for that?
Was This Post Helpful? 0
  • +
  • -

#24 Guest_Melody Heart Bontuyan*


Reputation:

Posted 25 June 2010 - 12:16 AM

Good day!

First and foremost thanks for sharing it was very useful. However when I run it, and input for example 6-1, the answer is 7 not 5. I did another try like 18/2 but instead of getting 9, it gave me 20. It seems like it adds up the inputs I give. I don't know what's wrong with the codes. I hope you could help me. Thanks!


Dim a  As Single        'declares the first input
Dim b  As Single        'declares the second input
Dim c  As Single        'declares the answer

Private Sub cmd0_Click()  'Put the value 0 into the txtInput text box
txtInput.Text = txtInput.Text + "0"
End Sub

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

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

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

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

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

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

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

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

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

Private Sub cmdAdd_Click() 'Addition button is selected
d = 1                      'We will use this for the case statements
a = Val(txtInput.Text)     'First input
txtInput.Text = " "
End Sub

Private Sub cmdClear_Click() 'To Clear the Input and Output screen
txtInput.Text = " "
txtResult.Text = "Answer"
End Sub

Private Sub cmdDivide_Click() 'Division button is selected
d = 4                         'We will use this for the case statements
a = Val(txtInput.Text)        'First input
txtInput.Text = " "
End Sub

Private Sub cmdDot_Click()    'Decimal point
txtInput.Text = txtInput.Text + "."
End Sub

Private Sub cmdEqual_Click()
b = Val(txtInput.Text)        'Second input
Select Case d                 'Case statements
Case d = 1
c = a + b
Case d = 2
c = a - b
Case d = 3
c = a * b
Case d = 4
c = a / b
End Select
txtResult.Text = c              'Displays the answer in txtResult text box
End Sub

Private Sub cmdMultiply_Click() 'Multiplication button is selected
d = 3                           'We will use this for the case statements
a = Val(txtInput.Text)          'First input
txtInput.Text = " "
End Sub

Private Sub cmdoff_Click()      'Ends the application
End
End Sub

Private Sub cmdSubtract_Click() 'Subtraction button is selected
d = 2                           'We will use this for the case statements
a = Val(txtInput.Text)          'First input
txtInput.Text = " "
End Sub

Was This Post Helpful? 0

#25 Mimyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-April 10

Posted 10 July 2010 - 06:42 AM

View Postsam_benne, on 30 April 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


when i write the code the program shows me that the + operator can't operate the number because there are string"Error 9 Operator '+' is not defined for types 'System.Windows.Forms.TextBox' and 'String'."
Was This Post Helpful? 0
  • +
  • -

#26 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 12 July 2010 - 09:55 AM

your using VB.NET not VB6

This post has been edited by NoBrain: 12 July 2010 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#27 Guest_evita*


Reputation:

Posted 05 November 2010 - 07:08 AM

it's very help..so thankful
Was This Post Helpful? 0

#28 Guest_ERIC*


Reputation:

Posted 07 December 2010 - 08:03 AM

this code didn't work..do i have to change something in the properties of the text box? or what?
Was This Post Helpful? 0

#29 iiNarbz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-January 11

Posted 02 January 2011 - 06:14 PM

Thanks for this!, Helped me very much.
Was This Post Helpful? 0
  • +
  • -

#30 akefalos  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 57
  • Joined: 23-May 11

Posted 23 May 2011 - 07:54 PM

View Postsam_benne, on 07 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.


what is manswer, mfirst, mbutton, msecond? i keep getting errors saying they are not declared and same with minusvalue
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2