|
-
Sep 16th, 2001, 10:33 PM
#1
Thread Starter
Lively Member
One instance of app
How do i keep a user from opening more than one of the same vb application? I want only one to be able to be run at a time, unlike msword that can run multiple times.
Last edited by chongo 2002; Sep 14th, 2009 at 02:48 PM.
-
Sep 16th, 2001, 10:43 PM
#2
Hyperactive Member
If App.PrevInstance = True Then
Unload Me
End If
-
Sep 17th, 2001, 01:49 AM
#3
PowerPoster
Hi
I got this code on this forum somewhere. It is better than using the App.Previnstance / unload method because it actually jumps to the currently running instance of ur project.
Regards
Stuart
VB Code:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal _
hwnd As Long) As Long
Public Const GW_HWNDPREV = 3
Public Sub ShowPrevInstance()
On Error Resume Next
Dim OldTitle As String
Dim ll_WindowHandle As Long
'saving the current title in OldTitle variable and changing the application title
OldTitle = App.Title
App.Title = "New App - This App Will Be Closed"
'finding the previous instance. if you are using VB 5.0 change "ThunderRT6Main" to "ThunderRT5Main"
ll_WindowHandle = FindWindow("ThunderRT6Main", OldTitle)
'if there is no old instances of your application - exit.
If ll_WindowHandle = 0 Then Exit Sub
'Find the window we need to restore
ll_WindowHandle = GetWindow(ll_WindowHandle, GW_HWNDPREV)
'Now restore it
Call OpenIcon(ll_WindowHandle)
'And Bring it to the foreground
Call SetForegroundWindow(ll_WindowHandle)
Unload Me
Set [Your Form Name Here] = Nothing
End Sub
-
Sep 17th, 2001, 05:44 AM
#4
Thread Starter
Lively Member
-
Sep 19th, 2001, 10:25 PM
#5
Thread Starter
Lively Member
On the last line does the title go inside [] or did you just put those there as markers? show me an example just to make sure i understand.
-
Sep 20th, 2001, 02:56 AM
#6
PowerPoster
Hi
Just insert the name of the form that loads at start up in ur app
So, if it is form1 do...
Set Form1 = Nothing
You dont even need this line in all cases but does no harm
Regards
Stuart
-
Sep 20th, 2001, 05:36 AM
#7
Thread Starter
Lively Member
-
Sep 20th, 2001, 09:03 PM
#8
Thread Starter
Lively Member
Ok the code works in the case that it willnot allow another application to open but it does not popup the running application do you know why?
-
Sep 21st, 2001, 03:12 AM
#9
PowerPoster
Hi again
It works but i hope that you actually called the routine...
In ur Sub Main or Form Load or wherever your project starts u need to call the routine
VB Code:
If App.PrevInstance Then ShowPrevInstance
Regards
Stuart
-
Sep 21st, 2001, 01:39 PM
#10
Thread Starter
Lively Member
Thanks that code works great
-
Sep 24th, 2001, 10:07 AM
#11
Addicted Member
I don't know about VB6, but VB5's class window name is "ThunderRT5Form", NOT "ThunderRT5Main".
NOTE: You can find the classname of any window using "Find window" option in SPY++ (bundled with VC++). It's very easy and it's helped me out alot recently. 
::Sibby::
If you can think it....you can code it....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|