Results 1 to 4 of 4

Thread: object moving help(vb 6)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    8

    object moving help(vb 6)

    i need help in thisss. i have a task that to move a Button in vb6 to move that in rectangular motion ..
    i m able to move it only once to move it in rectangular path.. soo i need hellp to move it constantly on the form if any1 can plz help??


    here is the code frm code window

    Private Sub Timer1_Timer()
    Dim a
    a = Command1
    If Command1.Top > 5000 Then
    Timer1.Interval = 0
    Else
    Command1.Top = Command1.Top + 100
    End If
    End Sub


    Private Sub Timer2_Timer()
    If Timer1.Interval = 0 Then
    Command1.Left = Command1.Left + 100
    End If
    End Sub

    Private Sub Timer3_Timer()
    If Command1.Left > 6000 Then
    Timer2.Interval = 0
    End If
    End Sub

    Private Sub Timer4_Timer()
    If Timer2.Interval = 0 Then
    Command1.Top = Command1.Top - 100
    End If
    End Sub

    Private Sub Timer5_Timer()
    If Command1.Top < 240 Then
    Timer4.Interval = 0
    End If
    End Sub

    Private Sub Timer7_Timer()
    If Timer4.Interval = 0 Then
    Command1.Left = Command1.Left - 100
    End If
    End Sub


    Private Sub Timer8_Timer()
    Timer7.Interval = 0
    test
    End Sub

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: object moving help(vb 6)

    Can't really tell what you are trying to do but you surely do not need 8 timers to do it.

    1 timer and a little conditional logic should do the trick.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: object moving help(vb 6)

    You only need 1 timer, not 8.

    1) Use a static or form-level variable to keep track of current direction the button is moving
    2) Based on that direction, check for movement boundary & change direction as needed
    Code:
    Dim m_Direction As Long
    
    Private Sub Timer1_Timer()
       Select Case m_Direction
       Case 0 ' left
           If Command1.Left - 100 =< 240 Then
               Command1.Left = 240
               m_Direction = m_Direction + 1 ' change directions
           Else 
               Command1.Left = Command1.Left - 100
           End If
       Case 1 ' up
    ... fill in this part
       Case 2 ' right
    ... fill in this part
       Case 3 ' down
           If Command1.Top + 100 >= 5000 Then
               Command1.Top = 5000 
               m_Direction = 0 ' start over
           Else 
               Command1.Top = Command1.Top + 100
           End If
       End Select
    End Sub
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: object moving help(vb 6)

    Try this
    vb Code:
    1. Private Enum MovingDirection
    2.     MovingUp
    3.     MovingRight
    4.     MovingDown
    5.     MovingLeft
    6. End Enum
    7.  
    8. Private Type MovingBound
    9.     MinTop As Long
    10.     MinLeft As Long
    11.     MaxTop As Long
    12.     MaxLeft As Long
    13.     MovingStep As Long
    14. End Type
    15.  
    16. Private CurrentDirection As MovingDirection
    17. Private MovingArea As MovingBound
    18.  
    19. Private Sub Form_Load()
    20.     CurrentDirection = MovingUp
    21.     MovingArea.MinTop = 1000
    22.     MovingArea.MinLeft = 1000
    23.     MovingArea.MaxTop = 4000
    24.     MovingArea.MaxLeft = 6000
    25.     MovingArea.MovingStep = 200
    26.    
    27.     Command1.Top = MovingArea.MinTop
    28.     Command1.Left = MovingArea.MinLeft
    29.    
    30.     Timer1.Interval = 100
    31.     Timer1.Enabled = True
    32. End Sub
    33.  
    34. Private Sub Timer1_Timer()
    35.     If CurrentDirection = MovingUp Then
    36.         Command1.Top = Command1.Top - MovingArea.MovingStep
    37.         If Command1.Top <= MovingArea.MinTop Then
    38.             CurrentDirection = MovingRight
    39.         End If
    40.    
    41.     ElseIf CurrentDirection = MovingRight Then
    42.         Command1.Left = Command1.Left + MovingArea.MovingStep
    43.         If Command1.Left >= MovingArea.MaxLeft Then
    44.             CurrentDirection = MovingDown
    45.         End If
    46.    
    47.     ElseIf CurrentDirection = MovingDown Then
    48.         Command1.Top = Command1.Top + MovingArea.MovingStep
    49.         If Command1.Top >= MovingArea.MaxTop Then
    50.             CurrentDirection = MovingLeft
    51.         End If
    52.    
    53.     ElseIf CurrentDirection = MovingLeft Then
    54.         Command1.Left = Command1.Left - MovingArea.MovingStep
    55.         If Command1.Left <= MovingArea.MinLeft Then
    56.             CurrentDirection = MovingUp
    57.         End If
    58.     End If
    59. End Sub



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width