Answered Program Leave?

Zephlon

Member
Joined
May 18, 2009
Messages
10
Programming Experience
1-3
I have tried many things, but the form's Leave/Deactivate events don't work. I have a program that has multiple forms open at once (one of them is a tool window), and I want to be able to detect when the user switches to a different program's window.
My program is a game, and I want it to pause automatically when not being played.

Can anyone help? Thanks in advance.
 
Last edited:
But will the event be triggered when the user uses a window in the same program?
 
Yes. VB.net would only pay attention to the form that actually lost focus. When the form loses focus, you can have the program call the pausing function/sub. When the form regains focus, have it resume. It doesn't matter if you have 100 forms from the same program open, the code is form specific.
 
So, is there a way to know when the user goes to a different program's window?
 
I tried this,
VB.NET:
    Private Sub TMautopause_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMautopause.Tick
        If Not Process.GetCurrentProcess.ProcessName = "Tick-Tack-Ton.vshost" Then
            Pause(True)
        End If
    End Sub
but it didn't pause when I switched to a different window. Is there a such thing as "GetActiveProcess"?
 
GetCurrentProcess returns the Process instance of calling application, ie not relevant for external processes.

Try this:
VB.NET:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If ActiveForm IsNot Nothing Then
        Static game As Integer
        game += 1
        Me.Text = game.ToString
    End If 
End Sub
 
...


:eek: Awesome! The number stops incrementing when you change the form.

I works almost perfectly, but I'm sure that it's a different part interfering.
 
Back
Top