Results 1 to 8 of 8

Thread: hi timers and loops

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    6

    hi timers and loops

    Hi I am new to vb and I am using vb 2008 I am trying to learn loops and timers but I have some problems.


    Here is my code

    Code:
    'application to send keys to
            AppActivate("notepad")
            'send key1
            SendKeys.Send("1")
            'wait 2 second
            Timer1.Enabled = True
            'send key2
            SendKeys.Send("2")
            'wait 2 second
            Timer1.Enabled = True
            'send key 3
            SendKeys.Send("3")
    The problem with this is it does not wait it just sends 123 all at the same time.
    I tryed using the other way the one that makes the program freez for the amount of time I want it to wait, but I dont want the program to freez I want to bealbe to move my mouse ext whilst its doing it.

    as for the loop this is as far as I got I cant really test it yet cos my program is typing 123 a million times a second and my pc doesnt like that so i need to fix the timers.

    Code:
          Do Until Button2 Is AcceptButton
                'application to send keys to
                AppActivate("notepad")
                'send key1
                SendKeys.Send("1")
                'wait 1 second
                Timer1.Enabled = True
                'send key2
                SendKeys.Send("2")
                'wait 1 second
                Timer1.Enabled = True
                'send key 3
                SendKeys.Send("3")
            Loop
        End Sub
    any help would be much apprecated thanks

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: hi timers and loops

    I think the problem is you are misunderstanding how events (in general and timers in particular) work.

    Basically you need to set an appropriate interval for the delay - the interval property accepts a number of milliseconds, so an interval of 1000 = 1 seconds.

    The other problem is that you don't just set the timer and then carry on, you need to put your code which should execute after the interval inside the timer control's tick event.

    ie :

    start notepad
    send first key
    enable timer

    then in timer's tick event

    send 2nd key

    Your problem is that you have several keys being sent at intervals, so either you need to have a number of timers each one being kicked off in sequence as the previous one executes, or more likely you need to have a counter variable incremented each time the timer tick event is fired and work out which key you need to send.

    Your outer loop makes no sense - you cant loop until a button is an "accept button" - I assume you want to loop until someone clicks a button and then exit?

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: hi timers and loops

    ok thanks I will try again.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: hi timers and loops

    Ok here is what I did but they keep sending the key I only want them to send the keys once and then have the whole process loop. Am I doing it better this time? Youll have to forgive me I'm 11 I find this very challenging.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'application to send keys to
            AppActivate("notepad")
    
            SendKeys.Send("1")
            Timer1.Enabled = True
            Timer2.Enabled = True
          
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            SendKeys.Send("2")
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            SendKeys.Send("3")
        End Sub
    End Class

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: hi timers and loops

    I did it!

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'application to send keys to
            AppActivate("notepad")
    
            SendKeys.Send("1")
            Timer1.Enabled = True
    
    
            Timer2.Enabled = True
    
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            SendKeys.Send("2")
            Timer1.Stop()
    
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            SendKeys.Send("3")
            Timer2.Stop()
        End Sub
    End Class
    Now I'll try and figure out how to loop it

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: hi timers and loops

    OK timers fire repeatedly until you stop them, so if you set an interval of 1000 then the event will fire every 1 second.

    Take a look at this :

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'application to send keys to
            AppActivate("notepad")
    
            SendKeys.Send("1")
            Timer1.Enabled = True
            'Timer2.Enabled = False          DELETE THIS AND ENABLE IN TIMER1 TICK!
          
    
        End Sub
    
    
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            SendKeys.Send("2")
            Timer1.Enabled = False
            Timer2.Enabled = True
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            SendKeys.Send("3")
            Timer2.Enabled = False
        End Sub
    End Class

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: hi timers and loops

    Thanks very much for your help I now understand timere a lot better.

    Now I'm going to work on loops.

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    6

    Re: hi timers and loops

    Code:
    If Timer3.Enabled = False Then Timer1.Start()
    wow that was easy I love programming!

    thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width