Hi guys. i'm new to this forum and new to programming in general. ive had some classes of vb.net and im just starting to actually play around with vb. im trying to make a program that starts with an image in a certain position (in ths case a car) and then, after 3 secs the image keeps moving at a certain speed untill i press a button button i cnt get it to work. can any of u guys help?

Public Class Form1
    Dim B1C As Boolean = False
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles Car1.Click

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        carMovement(1)

    End Sub

    Public Sub carMovement(carNo As Integer)
        Dim initialCountdown As New Stopwatch
        initialCountdown.Start()
        Car1.Location = New Point(400, 350)
        If initialCountdown.ElapsedMilliseconds > 3000 Then

            If carNo = 1 Then
                For Value As Integer = 0 To 2
                    Dim carMoveSpeed As New Stopwatch
                    carMoveSpeed.Restart()
                    If carMoveSpeed.ElapsedMilliseconds > 500 Then
                        Value = Value - 1

                        Car1.Location = New Point(400, Car1.Location.Y + 40)
                    End If
                    If B1C = True Then
                        Exit For
                    End If
                Next
            End If
        End If

Recommended Answers

All 8 Replies

The first thing you have to realize is that form coordinates are a bit different as we normally use them.
They start at the top left corner of the form, being Point(0,0). Y goes down, X goes the normal way, from left to right.
As you programmed it on line 26, your car will stay at X position=400 and you add 40 to Y, meaning your car will act more like a lunar lander...

I'm not a VB user, but just asking...
is it OK to do animation in a cpu-bound loop like that, or is the use of a Timer mandatory rather than just recommended?

even if it is OK, won't that code just go

start stopwatch
set location
stopwatch is < 3000 (unless set location is world's slowest method)
skip if block
return

it kinda looks like OP wants Stopwatch to behave like a Timer

I have to add this. Go get the free Visual Studio before you get old ideas locked in that you have to unlearn.

More on topic, VB6 worked as an event driven system and you may have to add a doevents() after the graphics action to see it happen. Otherwise the events and calls pile up and are handled at a later time. I decline to write more about doevents() since it's well done.

Thanks ddanbe. The thing is that car goes to the initial coordinates i tell him to go but then it does not move at all

The thing is that car goes to the initial coordinates i tell him to go but then it does not move at all

OK, please see my previous post. I think you misunderstand Stopwatch.

As James pointed out, use a Timer istead of a Stopwatch. How to do that can for example be found here.

ok thanks ill whatch that video. but i kept trying and i tryed doing something like this

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Car1.Location = New Point(Car1.Location.X, Car1.Location.Y + 2)
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles Car1.Click

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Car1.Location = New Point(0, 0)
        Dim sw As New Stopwatch
        sw.Start()
        Do Until sw.ElapsedMilliseconds > 3000
            Car1.Location = New Point(Car1.Location.X, Car1.Location.Y)
        Loop
    End Sub
End Class

this worked in a console app (running the console.writeline() method instead of moving the picturebox) but then, when i try to run on an app with a Form the form jut doesnt appear on the time it would be waiting (3 secs). then it appears and works perfectly.. i didnt put anything after the wait time so it doesnt even matter, but it was just for demonstrational porpuses.

Thanx guys. i did it with a timer c:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.