I am a beginner and am trying to make a calculator, i need some help with the BuildNumber, could someone show me how I can use this method to add the button clicked to the string please.
Code:
Private Sub BuildNumber(Number As String)
'Adds a number to the expression string
End Sub
Private Sub cmd0_Click()
'add number to string
Call BuildNumber("0")
txtOutput.Text = "0"
End Sub
Private Sub cmd1_Click()
'add number to string
Call BuildNumber("1")
txtOutput.Text = "1"
End Sub
Private Sub cmd2_Click()
'add number to string
Call BuildNumber("2")
txtOutput.Text = "2"
End Sub
Private Sub cmd3_Click()
'add number to string
Call BuildNumber("3")
txtOutput.Text = "3"
End Sub
Private Sub cmd4_Click()
'add number to string
Call BuildNumber("4")
txtOutput.Text = "4"
End Sub
Private Sub cmd5_Click()
'add number to string
Call BuildNumber("5")
txtOutput.Text = "5"
End Sub
Private Sub cmd6_Click()
'add number to string
Call BuildNumber("6")
txtOutput.Text = "6"
End Sub
Private Sub cmd7_Click()
'add number to string
Call BuildNumber("7")
txtOutput.Text = "7"
End Sub
Private Sub cmd8_Click()
'add number to string
Call BuildNumber("8")
txtOutput.Text = "8"
End Sub
Private Sub cmd9_Click()
'add number to string
Call BuildNumber("9")
txtOutput.Text = "9"
End Sub
First off you should create your buttons as an array
Start of with cmdNum set the common properties of all the numbers you want this one should have a text property of "0" then copy it and paste it into your form it will ask you if you want to make it an array, click yes.
Now you have 1, repeat the copy/paste until you have 10 (indexes from 0 to 9)
now when you click on cmdnum you get
VB Code:
Private Sub cmdNum_Click(Index as Integer)
text1.text = text1.text & Index
end sub
Now whenever you click a number button it will concatenate the index of the button array to the string.
You'll have to add logic for it to take away the initial zero as well as all the math stuff.
I didnt want to use an array, I have used this BuildNumber method in another prog for a safe code, what I was trying to do was copy the method but I cant figure how I would go about it, heres the safe code prog I did to show you what I am talking about.
Code:
Option Explicit
'Declare required storage space
'Variables need to be seen by several procedures
Dim CodeEntered As String
Dim Digits As Integer
Private Sub BuildCode(pCodeEntered As String, pNumber As String)
'Clear screen for output
picOutput.Cls
'If six digits have not yet been added
If Digits < 6 Then
'Add number to code string
pCodeEntered = pCodeEntered & pNumber
'Increment number of digits entered
Digits = Digits + 1
End If
'If six digits have now been entered
If Digits = 6 Then
'Output code user entered
picOutput.Print pCodeEntered
'Prompt user to check code entered against actual code
picOutput.Print "Check your code now."
End If
End Sub
Private Sub cmdCheckCode_Click()
'Declare the actual safe code
Const CODE = "321123"
'If the code the user entered is the same as the actual code
If CodeEntered = CODE Then
'Output success message
picOutput.Print "Cracked it!"
'The code entered does not match the actual safe code
Else
'Output failure message
picOutput.Print "Try again!!!"
'Invite re-entry
picOutput.Print "Enter a six digit number"
End If
'Reset number of digits entered
'and code entered string ready for next attempt
Digits = 0
CodeEntered = ""
End Sub
Private Sub cmdAddOne_Click()
'Add the string "1" to the current code entered
Call BuildCode(CodeEntered, "1")
End Sub
Private Sub cmdAddTwo_Click()
'Add the string "2" to the current code entered
Call BuildCode(CodeEntered, "2")
End Sub
Private Sub cmdAddThree_Click()
'Add the string "3" to the current code entered
Call BuildCode(CodeEntered, "3")
End Sub
Private Sub cmdExit_Click()
'Terminate program
End
End Sub
Private Sub Form_Load()
'Show the form
Show
'Iniatilise
CodeEntered = ""
Digits = 0
'Prompt user for action
picOutput.Print "Enter a six digit number"
End Sub
I can see what you mean about it being a lot easier, I will soldier on though, you should see the whole of my code, its a shambles, I didnt dare post the whole thing. Anyway it made more sense to me concentrating on just one area and getting that to work.