Animating a Picturebox's movement?

Moritain

Newcomer
Joined
Dec 2, 2005
Messages
6
Hello,

I’m a sophomore student working towards a CS degree and I’m having problems with VB.net. I’m working on an assignment to create a Vending Machine App. I’ve got the whole thing ironed out but can’t seem to animate the dropping of the can.

What I’m trying to do is capture the location of a PictureBox and then make a another PictureBox move down 20 pixels every 100ms. The effect I’m hoping for is a kinda-sorta animation of a can falling into to the bottom of the machine.

I’m using this Sub as my pause:

Private Sub Wait(ByVal Time As Integer)

For X As Integer = 1 To Time
System.Threading.Thread.CurrentThread.Sleep(100)
Application.DoEvents()
Next
End Sub

This is the best trial I have had so far after several hours of trying code samples from the net:

Private Sub DropTheCan()
picCanDrop.Visible = True
Do While picCanDrop.Location.Y < 360
picCanDrop.Location = New Point(picCanDrop.Location.Y - 20)
Loop
End Sub

I get a flicker, albeit, in the wrong location.

Any help would be greatly appreciated.

Thank You.
 
I haven't tried your code but after looking at it I'm slightly confused by this line
Visual Basic:
cCanDrop.Location = New Point(picCanDrop.Location.Y - 20)

I'm assuming theres some kind of typo here for 2 reasons. Firstly surely there should be an x and y coordinate specified in the new point. Secondly and perhaps the thing causing you trouble, as you are deducting from the Y value it will actually move upwards (since 0 is the top of the page) and thus will always be less than 360. Obviously that could also be a typing mistake but I thought I'd mention it.

You could use the timer object like so to achieve the effect your after...

Visual Basic:
    Public iCount As Int16
    Public iOld As Int16

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Timer1.Interval = 250
        iOld = Button1.Top
        Timer1.Start()

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Button1.Top += 10
        iCount += 1

        If iCount = 10 Then
            Timer1.Stop()
            Button1.Top = iOld
        End If

    End Sub

EDIT:- Obviously my example moves a button not picturebox, but thats kinda irrelevant.
 
Sorry for the slow reply, its been a crazy this weekend. Thanks a ton, that code put me on the right track and with a few minor changes worked awesomely.

Thanks again!
 
Back
Top