Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake buttons jump randomly around the screen in Visual Basic 6
DescriptionThis example shows how to make buttons jump randomly around the screen in Visual Basic .NET.
Keywordsbutton, jump, random, game, Visual Basic 6
CategoriesVB.NET, Miscellany, Puzzles and Games
 
The program starts by executing Sub Main. This routine creates 20 instances of Form1 and displays them.
 
Sub Main()
Dim i As Integer
Dim frm As Form1

    For i = 1 To 20
        Set frm = New Form1
        frm.Show
    Next i
End Sub
 
When a form loads, it moves its button to the form's uppper left corner and sizes the form to fit the button. It picks a random interval for its timer, enables the timer, and invokes the timer's Click event for the first time.
 
Private Sub Form_Load()
    Randomize
    cmdClickMe.Move 0, 0
    Me.Width = cmdClickMe.Width
    Me.Height = cmdClickMe.Height

    tmrMoveForm.Interval = 200 + Rnd * 600
    tmrMoveForm.Enabled = True

    tmrMoveForm_Timer
End Sub
 
When a form's timer fires, it moves the form to a new random position.
 
Private Sub tmrMoveForm_Timer()
Dim x As Single
Dim y As Single

    x = Int(Rnd * (Screen.Width - Me.Width))
    y = Int(Rnd * (Screen.Height - Me.Height))
    Me.Move x, y
End Sub
 
If the user manages to click a button, the Click event handler unloads all of hte program's forms.
 
Private Sub cmdClickMe_Click()
Dim frm As Form

    For Each frm In Forms
        Unload frm
    Next frm
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated