ahammar
asked on
Show form from App.PrevInstance
I have an app that is hidden. I want the form to show itself if the user tries to open the app again without openning another instance of the app.
Is this possible?
I tried:
If App.PrevInstance = True Then
End
Me.Show
Exit Sub
End If
That doesn't work.
Anyone know how I can do this.
Thanks and Cheers!
ahammar
Is this possible?
I tried:
If App.PrevInstance = True Then
End
Me.Show
Exit Sub
End If
That doesn't work.
Anyone know how I can do this.
Thanks and Cheers!
ahammar
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
BrianGEFF917:
I got your code to work fine. I do have a question though. Is your code opening up the new instance and killing the original one, or killing the new instance and just showing the form of the original one. I can't tell. It looks like it's opening the new instance and killing the original, but like I said, I can't tell. Even if it is, I can't see where that will be a problem yet, but I will have to test just a little more. Thanks for your help.
Hello Idle Mind
I tried your code, but I couldn't get it to work. I don't know why, but it just doesn't show anything when I try to open another instance of the app. Thanks for your help too.
Cheers!
ahammar
I got your code to work fine. I do have a question though. Is your code opening up the new instance and killing the original one, or killing the new instance and just showing the form of the original one. I can't tell. It looks like it's opening the new instance and killing the original, but like I said, I can't tell. Even if it is, I can't see where that will be a problem yet, but I will have to test just a little more. Thanks for your help.
Hello Idle Mind
I tried your code, but I couldn't get it to work. I don't know why, but it just doesn't show anything when I try to open another instance of the app. Thanks for your help too.
Cheers!
ahammar
ASKER
Oh...ok never mind. I see what's happening now. It is killing the latest one openned and leaving the original one. That's good. I will have to give you the points to BrianGEFF917 this go around. Thanks for your help!!
Sorry Idle_Mind, but BrianGEFF917 was the first one with a working solution this time. Thank you for the time you took to comment with a potential solution. I appreciate every attempt to help me!
Cheers!
ahammar
Sorry Idle_Mind, but BrianGEFF917 was the first one with a working solution this time. Thank you for the time you took to comment with a potential solution. I appreciate every attempt to help me!
Cheers!
ahammar
No need to be sorry! =)
I made it clear that my code wasn't much different than the first post and I didn't really expect points...
I made it clear that my code wasn't much different than the first post and I didn't really expect points...
Sorry it took me so long to get back to this. First thank you for the points. Secondly, if the caption of the form will change OR another form may be loaded you can do something similar to this by the EXE name. If you're intrested in that let me know.
Both assume that your programs caption does NOT change.
Option Explicit
Private Const SW_SHOWNORMAL = 1
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Sub Form_Load()
Dim myCaption As String
' set this value to what you want your main forms caption to be
myCaption = "MYPROGRAM"
If App.PrevInstance = True Then
' make sure this window is hidden
Me.Hide
DoEvents
' find the previous instance window based on the myCaption value set above
Dim prevHwnd As Long
prevHwnd = FindWindow(vbNullString, myCaption)
' if we found it then make it the foreground window in normal state
If prevHwnd <> 0 Then
ShowWindow prevHwnd, SW_SHOWNORMAL
SetForegroundWindow prevHwnd
End If
' kill this instance
End
End If
' no previous instance, show the window and set its caption
Me.Show
DoEvents
Me.Caption = myCaption
End Sub