p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Stephens' Visual Basic Programming 24-Hour Trainer (http://p2p.wrox.com/book-stephens-visual-basic-programming-24-hour-trainer-621/)
-   -   Lesson 4 Exercise 2 (http://p2p.wrox.com/book-stephens-visual-basic-programming-24-hour-trainer/86600-lesson-4-exercise-2-a.html)

Rennuke February 8th, 2012 05:52 PM

Lesson 4 Exercise 2
 
I wrote all the code for it ( I think [:)] ) and it doesn't show any errors in the Error list but when i try to debug the program An error window pops up ans says: {Error While trying to run project: Could not file or assembly "Timer's Tick" or one of its dependencies. The givan assembly name or codebasee was invalid. (Exception from HRESULT: 0x80131047)}

Here is the code
Code:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False
        Timer2.Enabled = True
        btnStop.Location = New Point(10, 10)
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Timer1.Enabled = True
        Timer2.Enabled = False
        btnStop.Location = New Point(20, 20)
    End Sub
End Class


Rod Stephens February 9th, 2012 04:54 PM

Offhand I don't see anything wrong with that code.

Note that you shouldn't need to type most of it. If you double-click on the timers, the code editor should open up and create empty event handlers for you. Then you only need to fill in the middle.

My guess is perhaps the timer's aren't named Timer1 and Timer2 on the form. Open the form in the designer and check their names. They must match what's shown in the "Handles" clauses. If they don't match, either rename the controls or change the code so they match.

If that doesn't work, zip up the project and send it to me (RodStephens@vb-helper.com) and I'll take a look at it.

TheMysticRuler January 17th, 2013 08:28 PM

While were on the subject, why does the code not work if the code in the handlers is switched around (swapped)?
Original code left in comments.

Code:

' Move the button to (10, 10), disable tmrMoveLeft, and enable tmrMoveRight.
    Private Sub tmrMoveLeft_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMoveLeft.Tick
        'tmrMoveLeft.Enabled = False
        'tmrMoveRight.Enabled = True

        tmrMoveLeft.Enabled = True
        tmrMoveRight.Enabled = False
        btnStop.Left = 10
        btnStop.Top = 10
    End Sub

    ' Move the button to (200, 200), disable tmrMoveRight, and enable tmrMoveLeft.
    Private Sub tmrMoveRight_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMoveRight.Tick
        'tmrMoveLeft.Enabled = True
        'tmrMoveRight.Enabled = False

        tmrMoveLeft.Enabled = False
        tmrMoveRight.Enabled = True
        btnStop.Left = 200
        btnStop.Top = 200
    End Sub

It only seems to work once; to move the button to (10,10), but then it stops.

Being completely new to VB programming, I dont know how to step through the code; or for that matter know what order it goes about executing.

Thanks for any help.

Rod Stephens January 17th, 2013 08:49 PM

It looks like the timers are disabling themselves instead of each other. It should work this way:

Right Timer:
Disable right timer
Enable left timer

Left Timer:
Disable left timer
Enable right timer

That way they basically take turns running. So right fires and enables left. Then left fires and enables right.

I think the way the code is here, they enables themselves and disable the other timer. So if left is the first to go, it enables itself, moves the button, and disables right. Left fires again but the button is already in that spot so you don't see it move.

(Setting breakpoints in the code is an important skill and you should get some practice at it but it's hard to do with timers because their behavior is tied to the clock. If you stop the code, time passes while you step through the code and that can do weird things to the timers' timing. You might be able to do it in this example but only one timer is enabled at a time, but in general stepping through code that is fired by timers is tricky.)


All times are GMT -4. The time now is 07:50 PM.

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.