[RESOLVED] Using For Next Loops To Properly Shift a PictureBox up or Down-VBForums
Results 1 to 3 of 3

Thread: [RESOLVED] Using For Next Loops To Properly Shift a PictureBox up or Down

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    Resolved [RESOLVED] Using For Next Loops To Properly Shift a PictureBox up or Down

    Hiya,

    Ive been struggling for a while trying to figure how out to Properly Move an object up or down using a For next loop, here is my current set up
    Code:
     Private Sub Shift()
    
            For intX_direction = 0 To Me.ClientSize.Width
    
                picBall.Left = intX_direction
            Next
    
            For intY_direction = 0 To Me.ClientSize.Height
    
                picBall.Top = intY_direction
            Next
    and that code is then called upon in a Do loop
    Code:
       Private Sub picBall_Click(sender As Object, e As EventArgs) _
            Handles picBall.Click
            intX_direction = _RIGHT
            intY_direction = _UP
            Do
                Shift()
    
                If picBall.Left + picBall.Width > Me.Width Then
                    intX_direction -= intX_direction
    
                ElseIf picBall.Left + picBall.Width < Me.Width Then
                    picBall.Left = 0
    
                End If
    
                If picBall.Top + picBall.Height > Me.Height Then
                    picBall.Top = intY_direction * -1
    
                End If
    
            Loop Until picBall.Left < 0
    I apologize for my lack of knowledge before hand, Im still learning
    Last edited by Shaggy Hiker; Oct 27th, 2017 at 07:32 PM. Reason: Added CODE tags.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,003

    Re: Using For Next Loops To Properly Shift a PictureBox up or Down

    Welcome to the forums. I edited your post to add [CODE][/CODE] tags. You can do this by pressing the # button and pasting code between the tags.

    As to the question, it's unlikely that a for loop of any sort will do what you are hoping for, no matter what that is. For loops run too fast, and while they are running they keep the UI thread from dealing with messages. The key message is the Paint event. If that isn't getting handled, then you won't see anything happen until the loop has finished, at which point the PB will just appear to jump to the final location.

    You could fix this to some extent by forcing the UI to redraw the form by calling picBall.Refresh, but that won't help all that much, because the motion will still be too fast to be meaningful.

    The best solution is to put a timer component on the form. The Timer is found in the toolbox, so drag one onto the form. You can then set the interval of the timer, which is in milliseconds (and don't bother going below about 100), then double click the timer to go to the Tick event handler, which is the one event raised by the timer. That event will be raised every time the interval elapses. That becomes your loop.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    4

    Re: Using For Next Loops To Properly Shift a PictureBox up or Down

    Hiya, sorry for the late response but I was able to get what i needed with a for next loop and a counter but i appreciate your help

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.