Results 1 to 9 of 9

Thread: Auto Stop

  1. #1

    Thread Starter
    Member
    Join Date
    May 2013
    Posts
    57

    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.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Auto Stop

    Timers don't take focus. They are not controls.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Auto Stop

    Oh he meant the form.....my bad.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Auto Stop

    What he is asking to do is pretty easy to accomplish:-
    vbnet Code:
    1. '
    2.     Private Sub Form1_Activated(sender As Object, e As System.EventArgs) Handles Me.Activated
    3.         Timer1.Stop()
    4.     End Sub
    5.  
    6.     Private Sub Form1_Deactivate(sender As Object, e As System.EventArgs) Handles Me.Deactivate
    7.         Timer1.Start()
    8.     End Sub

    The above code will stop a Timer when the form has "focus" and start it back when it doesn't.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Auto Stop

    Quote Originally Posted by Shaggy Hiker View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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