'Long Division, v.1.0, (c) 2006 Peter Browne
'PPPPPP RRRRRR BBBBB
'PP PP RR RR BB B
'PPPPPP RRRRRR BBBBBB
'PP RR RR BB BB
'PP RR RR BBBBBB
'License: GPL (GNU General Public License), v.2
'http://www.gnu.org/licenses/gpl.txt
'Declare the Variables
Dim Dividend, Divisor, StepOneAnswer, StepTwoAnswer, StepThreeAnswer As Single
Dim StepCounter As Integer
Dim FinalAnswer As Double
Function ResetOrOpen()
'Hide the Divisor and Dividend
lblDivisor.Visible = False
lblDividend.Visible = False
'Show the Text/Input Boxes
txtDivisor.Visible = True
txtDividend.Visible = True
'Enable the user to Start or Clear the Boxes
cmdStart.Enabled = True
cmdClear.Enabled = True
cmdRestart.Enabled = False
'Show the buttons to Start or Clear
cmdStart.Visible = True
cmdClear.Visible = True
cmdRestart.Enabled = False
'cmdStart.Left = 720
'cmdStart.Top = 6600
'cmdClear.Left = 2160
'cmdClear.Top = 6600
End Function
Private Sub form_load()
ResetOrOpen
cmdNext.Enabled = False
End Sub
Function GenerateQuestion()
'If the Divisor is blank is or 0 then give an error
If txtDivisor.Text = "" Or txtDivisor.Text = "0" Then
MsgBox ("You need to enter a Divisor, it cannot be 0.")
End If
'If there is no dividend, give an error
If txtDividend.Text = "" Then
MsgBox ("You need to enter a Dividend")
End If
'Don't let the user click Start or Clear, and hide the buttons
cmdStart.Visible = False
cmdClear.Visible = False
cmdStart.Enabled = False
cmdClear.Enabled = False
'cmdStart.Top = 0
'cmdStart.Left = 0
'cmdClear.Top = 0
'cmdClear.Left = 0
'Set the Divisor and Dividend to the corresponding variables
Divisor = txtDivisor.Text
Dividend = txtDividend.Text
'We should hide the text/input boxes, but show the labels
txtDivisor.Visible = False
txtDividend.Visible = False
lblDivisor.Visible = True
lblDividend.Visible = True
'Show the Divisor and Dividend in the correspoding boxes
lblDivisor.Caption = Divisor
lblDividend.Caption = Dividend
StepCounter = 1
cmdNext.Enabled = True
End Function
Private Sub txtDividend_KeyDown(KeyCode As Integer, Shift As Integer) 'When something is entered into txtDividend
'If the input is not a number, clear it and display an error
If KeyCode > 57 And KeyCode < 96 Then
MsgBox ("Numbers Only Please!")
KeyCode = 0
txtDividend.Text = ""
End If
If KeyCode > 105 Then
MsgBox ("Numbers Only Please!")
KeyCode = 0
txtDividend.Text = ""
End If
End Sub
Private Sub txtDivisor_KeyDown(KeyCode As Integer, Shift As Integer) 'When something is entered into txtDivisor
'If the input is not a number, clear it and display an error
If KeyCode > 57 And KeyCode < 96 Then
MsgBox ("Numbers Only Please!")
KeyCode = 0
txtDivisor.Text = ""
End If
If KeyCode > 105 Then
MsgBox ("Numbers Only Please!")
KeyCode = 0
txtDivisor.Text = ""
End If
End Sub
Private Sub cmdStart_Click()
GenerateQuestion
cmdRestart.Enabled = True
cmdRestart.Visible = True
End Sub
Private Sub cmdClear_Click()
'Let's Clear the Text Boxes when the user clicks Clear
txtDividend.Text = ""
txtDivisor.Text = ""
End Sub
Private Sub cmdRestart_Click()
ResetOrOpen
lblAnswer.Caption = ""
lblStepOne1.Caption = ""
lblStepOne2.Caption = ""
lblStepTwo1.Caption = ""
lblStepTwo2.Caption = ""
lblStepThree1.Caption = ""
lblStepThree2.Caption = ""
txtDivisor.Text = ""
txtDividend.Text = ""
End Sub
Private Sub cmdDone_Click()
'Let's close the program
Unload frmDivision
End Sub
Private Sub cmdNext_Click()
'When the user clicks on cmdNext, calculate FinalAnswer then proceed to the next Step
'FinalAnswer = Dividend / Divisor
'If we're at Step One, then follow do StepOne, and increase the StepCounter
If StepCounter = 1 Then
StepOne
StepCounter = StepCounter + 1
Exit Sub
End If
'Step Two
If StepCounter = 2 Then
StepTwo
StepCounter = StepCounter + 1
Exit Sub
End If
'Step Three
If StepCounter = 3 Then
StepThree
StepCounter = StepCounter + 1
Exit Sub
End If
If StepCounter = 4 Then
StepFour
Exit Sub
End If
End Sub
Function StepOne()
'Show the Step One Line
lineStepOne.Visible = True
'Divide the Dividend by the Divisor
'Lets try a different way
lblStepOne2.Caption = Dividend Mod Divisor
lblAnswer.Caption = (Dividend - lblStepOne2.Caption) / Divisor
lblStepOne1.Caption = "-" & Dividend - lblStepOne2.Caption
StepOneAnswer = Dividend / Divisor
End Function
Public Sub StepTwo()
If StepOneAnswer = 0 Then
Call Fin
lblStepTwo1.Caption = "Finished!" 'Show "Done" in the Step Two label
Exit Sub
ElseIf Divisor > StepOneAnswer Then
lineStepTwo.Visible = True 'Show the Step Two Line
lblAnswer.Caption = lblAnswer.Caption & "."
lblDividend.Caption = Dividend & ".0"
StepOneAnswer = Val(lblStepOne2.Caption & "0")
lblStepOne2.Caption = StepOneAnswer
lblStepTwo1.Caption = "-" & ((StepOneAnswer \ Divisor) * Divisor)
lblStepTwo2.Caption = StepOneAnswer - ((StepOneAnswer \ Divisor) * Divisor)
lblAnswer.Caption = lblAnswer.Caption & (StepOneAnswer \ Divisor)
StepTwoAnswer = Val(lblStepTwo2.Caption)
End If
End Sub
Public Sub StepThree()
If StepTwoAnswer = 0 Then
Fin
lblStepThree1.Caption = "Finished!"
Exit Sub
ElseIf Divisor > StepTwoAnswer Then
StepTwoAnswer = Val(lblStepTwo2.Caption & "0")
lblStepTwo2.Caption = StepTwoAnswer
lblStepThree1.Caption = "-" & ((StepTwoAnswer \ Divisor) * Divisor)
lineStepThree.Visible = True
lblStepThree2.Caption = StepTwoAnswer - ((StepTwoAnswer \ Divisor) * Divisor)
lblAnswer.Caption = lblAnswer.Caption & (StepTwoAnswer \ Divisor)
StepThreeAnswer = Val(lblStepThree2.Caption)
End If
End Sub
Public Sub StepFour()
'If the Answer from Step Three is Zero, then display a Finished Message Box
'Otherwise, show the answer.
If StepThreeAnswer = 0 Then
Fin
Exit Sub
Else
MsgBox ("This question has more than three steps, and I can only calculate the first three steps." & vbNewLine & vbNewLine & "But, I do know the final answer," & vbNewLine & "Which is: " & FinalAnswer)
Fin
lblAnswer.Caption = FinalAnswer
End If
'Disable the Next Step button
cmdNext.Enabled = False
End Sub
Function Fin()
'Display a Message Box to tell the user that the question is done, and that they can do another question
MsgBox ("Finshed!" & vbNewLine & "Click the Restart Button to do another question!")
End Function |