VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Enhancement to existing code entitled Just something fun to do.

by Ben Rigsby / Literary Arts (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 27th December 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Enhancement to existing code entitled "Just something fun to do."

Rate Enhancement to existing code entitled Just something fun to do.



'command buttons.  Have fun!

Private Sub Command1_Click()

'ALWAYS DOCUMENT YOUR CODE!

'*************************************************
'* This routine is a variation on the old novelty*
'* executive decision maker which, when shaken,  *
'* would return a random answer to any question! *
'*                    Have Fun!                  *
'*************************************************

Repeat: 'This label allows the "Random Generator"
        'to be called recursively, if necessary, to
        'avoid duplicate answers
    
    Randomize 're-seed the "Random Number Generator"
    
    'Use variable names that mean something!  Don't
    'just copy the "Microsoft Examples" and paste them
    'in without some reason for doing so...
    DecisionValue% = Int((6 * Rnd) + 1)
    
    'Select Case is a better choice in lieu of an "IF"
    'block here.  As you can see, I use much less code
    'to accomplish the same task.
    Select Case DecisionValue%
        Case 1
             LblAnswer = "Not today!"
             BackGroundColor% = 12
        Case 2
             LblAnswer = "Definately!"
             BackGroundColor% = 10
        Case 3
             LblAnswer = "Not today!"
             BackGroundColor% = 12
        Case 4
             LblAnswer = "Yes!"
             BackGroundColor% = 10
        Case 5
             LblAnswer = "No!"
             BackGroundColor% = 12
        Case 6
             LblAnswer = "My sources say maybe!"
             BackGroundColor% = 14
    End Select
    
    'This "IF" block makes sure it's never the same
    'answer twice in a row
    If LblAnswer = Label1.Caption Then 'if the same
        GoTo Repeat ' do over
    Else 'otherwise
        Label1.Caption = LblAnswer ' show the new answer
        Form1.BackColor = QBColor(BackGroundColor%) 'change the color
    End If
    
    'This one is unecessary!
    '*********************
    '*MyValue = Lblanswer*
    '*********************
    
    '   Last, but certainly not least, check your
    'spelling!  It's a sure mark of and inexperienced
    'programmer and, besides, it will make consumers
    'hesitant to rely on your applications, programmers
    'on your code, and any future customers on your products!
    
End Sub

Private Sub Command2_Click()

    'end the program
    End

End Sub

Private Sub Form_Load()

'Set up your default values here
Label1.Caption = "Ask a question..."

'set up some fun stuff like this timer...
Timer1.Interval = 1000 'about a second

End Sub

Private Sub Timer1_Timer()

    'when the timer fires, alternate the "Command"
    'button text
    If Command1.Caption = "Ask a question..." Then
        Command1.Caption = "...then click here!"
    Else
        Command1.Caption = "Ask a question..."
    End If
    
End Sub


Download this snippet    Add to My Saved Code

Enhancement to existing code entitled Just something fun to do. Comments

No comments have been posted about Enhancement to existing code entitled Just something fun to do.. Why not be the first to post a comment about Enhancement to existing code entitled Just something fun to do..

Post your comment

Subject:
Message:
0/1000 characters