|
-
Sep 5th, 2009, 04:27 PM
#1
Thread Starter
New Member
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
-
Sep 5th, 2009, 04:32 PM
#2
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?
-
Sep 5th, 2009, 04:36 PM
#3
Thread Starter
New Member
Re: hi timers and loops
ok thanks I will try again.
-
Sep 5th, 2009, 04:46 PM
#4
Thread Starter
New Member
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
-
Sep 5th, 2009, 04:52 PM
#5
Thread Starter
New Member
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
-
Sep 5th, 2009, 04:52 PM
#6
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
-
Sep 5th, 2009, 05:22 PM
#7
Thread Starter
New Member
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.
-
Sep 5th, 2009, 05:26 PM
#8
Thread Starter
New Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|