0

I am trying to create a simple winform application in VB. I have a form, a picturebox with a background image and a button. When I press the button, I want the picturebox to move left across the form until it disappears off the left side. Then I want it to reappear on the right side and move to the left again.

Initial condition:

initial condition

The problem seems to be that the areas of the picturebox that exceed the boundaries of the form get erased. So the first time around the images slides nicely off the left side of the form and never comes back to the right side. Or rather, it does come back, I just can see it.

Second scroll around, parts of image in picturebox that exceeded the boundaries of the form have been erased (left) or distorted (right):

distorted

In the code below I have deliberately changed the boundaries for the picturebox so that I can see that it is actually looping back around. But as you can see in this image, the parts of the picture box that exceeded the boundaries of the form are either erased or distorted.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Do
            PictureBox1.Left -= 1
            Threading.Thread.Sleep(2)

            If PictureBox1.Left <= -20 Then 'If PictureBox1.Left <= PictureBox1.Width Then
                PictureBox1.Left = Me.Width - 40 'PictureBox1.Left = Me.Width
            End If

        Loop
    End Sub
End Class

I have tried this with both VS2019 and VS2017 on two different computers and the same thing happens. I have also tried replacing the picturebox with a textbox and much the same thing happens.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

I changed my code to this, and now it works perfectly.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Timer1.Interval = 25
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        PictureBox1.Left -= 1

        If PictureBox1.Left <= -PictureBox1.Width Then
            PictureBox1.Left = Me.Width
        End If
    End Sub
End Class
mkrieger1
  • 19,194
  • 5
  • 54
  • 65