Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' Visual Basic Programming 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' Visual Basic Programming 24-Hour Trainer by Rod Stephens; ISBN: 978-0-470-94335-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' Visual Basic Programming 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old February 8th, 2012, 04:52 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Location: A pale blue dot....
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
__________________
Rennuke Atrawis
Reply With Quote
  #2 (permalink)  
Old February 9th, 2012, 03:54 PM
Rod Stephens's Avatar
Wrox Author
Points: 2,726, Level: 21
Points: 2,726, Level: 21 Points: 2,726, Level: 21 Points: 2,726, Level: 21
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 566
Thanks: 1
Thanked 75 Times in 74 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
  #3 (permalink)  
Old January 17th, 2013, 07:28 PM
Registered User
Points: 18, Level: 1
Points: 18, Level: 1 Points: 18, Level: 1 Points: 18, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Last edited by TheMysticRuler; January 17th, 2013 at 07:30 PM.
Reply With Quote
  #4 (permalink)  
Old January 17th, 2013, 07:49 PM
Rod Stephens's Avatar
Wrox Author
Points: 2,726, Level: 21
Points: 2,726, Level: 21 Points: 2,726, Level: 21 Points: 2,726, Level: 21
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Jan 2006
Location: , , .
Posts: 566
Thanks: 1
Thanked 75 Times in 74 Posts
Default

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.)
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 4 Try it Exercise caa5042 BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 13 February 2nd, 2013 02:32 PM
Lesson 8 - Try It Femi Sonuga BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 4 February 12th, 2012 07:44 AM
Lesson 4 Femi Sonuga BOOK: Knight's Microsoft Business Intelligence 24-Hour Trainer 0 November 25th, 2011 09:25 AM
Lesson 4 - Try It brookesmith BOOK: JavaScript 24-Hour Trainer 2 September 15th, 2011 05:40 PM
Lesson 19 Exercise 4 mpthemaster BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 3 June 11th, 2011 08:54 PM



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


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