Create a calculator
#16
Posted 30 May 2009 - 02:20 AM
#17
Posted 09 August 2009 - 10:21 AM
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
#18
Posted 10 August 2009 - 01:21 AM
#20
Posted 09 October 2009 - 03:15 AM
#21
Posted 11 October 2009 - 04:43 AM
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.
#22
Posted 31 October 2009 - 03:07 AM
kali1613, on 9 Oct, 2009 - 02:15 AM, said:
may be you did not assign the value calculated to the display variable
try this
txtNumber = manswer this goes on the equal sign button
kali1613, on 9 Oct, 2009 - 02:15 AM, said:
may be you did not assign the value calculated to the display variable
try this
txtNumber = manswer
this goes on the equal sign button
#23
Posted 26 May 2010 - 09:39 AM
#24 Guest_Melody Heart Bontuyan*
Posted 25 June 2010 - 12:16 AM
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
#25
Posted 10 July 2010 - 06:42 AM
sam_benne, on 30 April 2008 - 07:50 AM, said:
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.
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'."
#26
Posted 12 July 2010 - 09:55 AM
This post has been edited by NoBrain: 12 July 2010 - 09:57 AM
#28 Guest_ERIC*
Posted 07 December 2010 - 08:03 AM
#30
Posted 23 May 2011 - 07:54 PM
sam_benne, on 07 May 2008 - 11:42 AM, said:
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
|
|







MultiQuote




|