• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

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
0
ahammar
Asked:
ahammar
  • 2
  • 2
  • 2
1 Solution
 
BrianGEFF719Commented:
Here, try this, it will check if the program is runnign when it is loaded, if it is it will bring the other window to front and terminate the copy of the program that was just loaded. This code should be placed in your Form_Load()


Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Sub Form_Load()
 
 Dim lngHwnd As Long
 Dim otherHwnd As Long
 Dim boolOther As Boolean
 boolOther = False
 
 lngHwnd = FindWindow(vbNullString, Me.Caption)
 Do
 DoEvents
    If lngHwnd <> Me.hWnd Then boolOther = True
    If lngHwnd <> Me.hWnd Then otherHwnd = lngHwnd
       
    lngHwnd = FindWindowEx(0&, lngHwnd, vbNullString, Me.Caption)
 Loop While lngHwnd <> 0
 
 
 If (boolOther = True) And (otherHwnd <> 0) Then
    'bring the other one to front and terminate this one
    SetWindowPos otherHwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    End
 End If
 
 
End Sub

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*** This isn't that much different from BrianGEFF719s post.  ***

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
0
 
ahammarAuthor Commented:
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ahammarAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...
0
 
BrianGEFF719Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now