Berapa Choclate Yang Kita Makan Sampai Saat Ini
April 8, 2008
mmmm…berapa coklat ya yang sudah saya makan selama ini….??
Gunakan VB untuk menghitungnya…
Option Explicit
Dim strTmp As String
Private Sub Form_Load()
‘Set focus on a dummy control and move it out of view.
cmdDummy.TabIndex = 0
cmdDummy.Left = -1000
‘Disable option buttons.
optYes.Enabled = False
optNo.Enabled = False
‘Add items to combo box
With Combo1
.AddItem “2″
.AddItem “3″
.AddItem “4″
.AddItem “5″
.AddItem “6″
.AddItem “7″
.AddItem “8″
.AddItem “9″
End With
lblT1.Caption = “”
‘Make sure temp string is empty.
strTmp = “”
End Sub
Private Sub cmdDoIt_Click()
‘This is one way of checking if user has entered all necessary info.
‘In this example I’ve chose to disable inputs until user enters info.
If strTmp = “” Then
MsgBox “Please select the number of chocolates you’d like.”, vbInformation, “Chocolate Error”
Exit Sub
End If
If optYes.Value = False And optNo.Value = False Then
MsgBox “You need to select the Yes or No option.”, vbInformation, “Yes No Error”
Exit Sub
End If
If txtBYear.Text = “” Then
MsgBox “Please enter your four digit birth year.”, vbInformation, “Year Error”
Exit Sub
End If
‘Last calculation - See formula above.
strTmp = strTmp - txtBYear.Text
‘*Used to Test strTmp in IDE. Rem out if compiling
lblT1.Caption = strTmp
lblNumChoc.Visible = True
‘Gives first numeral (digit)
lblNumChoc.Caption = “You wanted to have chocolate ” & _
Left$(strTmp, 1) & ” times each week”
lblAge.Visible = True
‘Gives last two numerals (digit)
lblAge.Caption = “And you are ” & Right$(strTmp, 2) & ” years old.”
cmdDoIt.Visible = False
cmdClear.Visible = True
End Sub
Private Sub Combo1_Click()
DoMe
End Sub
Private Sub Combo1_DropDown()
optYes.Enabled = True
optNo.Enabled = True
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Set frmChocMath = Nothing
End Sub
Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)
End Sub
Private Sub optYes_Click()
‘See above formula.
If optYes.Value = True Then
optNo.Enabled = False
Combo1.Enabled = False
strTmp = strTmp + 1752
txtBYear.Enabled = True
End If
txtBYear.SetFocus
End Sub
Private Sub optNo_Click()
‘See above formula.
If optNo.Value = True Then
optYes.Enabled = False
Combo1.Enabled = False
strTmp = strTmp + 1751
txtBYear.Enabled = True
End If
txtBYear.SetFocus
End Sub
Private Sub txtBYear_Change()
‘Once four digits have been entered show cmdDoIt button
If Len(txtBYear.Text) = 4 Then cmdDoIt.Visible = True
End Sub
Private Sub txtBYear_KeyPress(KeyAscii As Integer)
‘Allows only numbers, DEL and Backspace keys in text box.
Select Case KeyAscii
Case 8, 127
‘Do nothing
‘This allow the user to press BackSpace
‘and Delete keys to correct his typing
Case 48, 49, 50, 51, 52, 53, 54, 55, 56, 57
‘These are the ASCII codes of the numbers
‘from 0 to 9.
‘Do not allow more than 4 digits
If Len(txtBYear.Text) >= 4 Then
KeyAscii = 0
End If
Case Else
‘Prevent all other characters from getting
‘into the text box
KeyAscii = 0
End Select
End Sub
Private Sub txtBYear_LostFocus()
‘Checks if user has entered a “reasonable” year of birth.
If txtBYear.Text < “1901″ Then
MsgBox “You should not be eating chocolates at your age !” _
& vbCrLf & “Go find someone who is a bit younger” & _
” and ask them to try this
“, vbExclamation, “You’re too old !”
GoTo OptOut
ElseIf txtBYear.Text > “2001″ Then
MsgBox “We are not playing “”Back to the Future”" here :-)” & vbCrLf & _
” Enter your correct four digit birth year.”, _
vbExclamation, “You’re too young !”
GoTo OptOut
Else
Exit Sub
End If
OptOut:
txtBYear.SelStart = 0
txtBYear.SelLength = Len(txtBYear.Text)
cmdDoIt.Visible = False
End Sub
Private Sub cmdClear_Click()
ClearMe
End Sub
Private Sub DoMe()
‘Combo array starts at 0 and ends 8, so the first index
‘would be equal to 0 but the value we want to use is 2
‘for index 0. So we add 2 to each index to get the
‘correct value.
strTmp = Combo1.ListIndex + 2
‘See the formula at the top to follow this. Dead easy!
strTmp = strTmp * 2
strTmp = strTmp + 5
strTmp = strTmp * 50
End Sub
Private Sub ClearMe()
‘Resets app back to default.
cmdDoIt.Visible = False
Combo1.Enabled = True
Combo1.Text = “Choc’s”
optYes.Value = False
optYes.Enabled = False
optNo.Value = False
optNo.Enabled = False
txtBYear.Text = “”
txtBYear.Enabled = False
lblNumChoc.Caption = “”
lblAge.Caption = “”
strTmp = “”
‘*Used to Test strTmp in IDE. Rem out if compiling.
lblT1.Caption = strTmp
cmdClear.Visible = False
cmdDummy.SetFocus
End Sub