Solved

Show form from App.PrevInstance

Posted on 2006-06-16
6
282 Views
Last Modified: 2010-04-07
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
Comment
Question by:ahammar
  • 2
  • 2
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 500 total points
ID: 16925389
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16926724
*** 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
 
LVL 23

Author Comment

by:ahammar
ID: 16928372
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 23

Author Comment

by:ahammar
ID: 16928387
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16928449
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
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16928899
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now