|
-
May 16th, 2013, 09:36 AM
#1
Thread Starter
Member
Auto Stop
I was wondering, how would I make my program stop Timer1 if it is in focus? I thought it may look like this
Code:
Private Sub AutoStop()
If Form1.Focused Then
Timer1.Stop()
End If
End Sub
But that is wrong.
-
May 16th, 2013, 10:39 AM
#2
Re: Auto Stop
Timers don't take focus. They are not controls.
-
May 16th, 2013, 10:45 AM
#3
Re: Auto Stop
I'm not sure what Niya is on about, as the timer is not the one dealing with focus as far as I can see.
However, that's a method, so it would have to be called from somewhere, which kind of defeats the purpose. It would be better if the form itself was what called the method, at the moment that it got focus...or something like that, which is what an event handler would be for. The problem you will run into is that Focus, whatever the target, is a pretty lousy event to deal with. You might look at the Form.Shown event, the Form.Activated event, and one other that I can't think of at the moment. They all work best in certain situations, so you'd have to determine what event you really want this to happen for.
My usual boring signature: Nothing
 
-
May 16th, 2013, 10:49 AM
#4
Re: Auto Stop
Oh he meant the form.....my bad.
-
May 16th, 2013, 10:58 AM
#5
Re: Auto Stop
What he is asking to do is pretty easy to accomplish:-
vbnet Code:
'
Private Sub Form1_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
Timer1.Stop()
End Sub
Private Sub Form1_Deactivate(sender As Object, e As System.EventArgs) Handles Me.Deactivate
Timer1.Start()
End Sub
The above code will stop a Timer when the form has "focus" and start it back when it doesn't.
-
May 16th, 2013, 11:41 AM
#6
Re: Auto Stop
I don't like the Activate event, though. It gets fired at weird times. On the other hand, the OP didn't explain anything about what the timer is all about, and without knowing that....anything might work.
My usual boring signature: Nothing
 
-
May 16th, 2013, 11:48 AM
#7
Re: Auto Stop
 Originally Posted by Shaggy Hiker
I don't like the Activate event, though. It gets fired at weird times....
Alternatively, you can poll using GetForegroundWindow but I don't like the idea of polling in general when you can avoid it.
-
May 16th, 2013, 12:20 PM
#8
Re: Auto Stop
Yeah, polling is pretty bad.
The event I was forgetting about is VisibleChanged. This one seems to be less sensitive than Activated, but more sensitive that Shown (which happens only once). If the form is being shown/hidden/shown/hidden, then VisibleChanged may be the way to go.
My usual boring signature: Nothing
 
-
May 16th, 2013, 01:33 PM
#9
Re: Auto Stop
Just stepping back and looking at this, I believe the OP may have some malicious intents here. The other post that the OP made are about sendkeys randomly using a timer, now the OP wants to start/stop the timer based on if the form's focused or not. I may be off, but that's definitely what it appears to be.
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
|