Solved

App.previnstance and command$

Posted on 1998-10-10
3
427 Views
Last Modified: 2012-05-04

When my program opens,


if command$ = "" and app.previnstance then
unload
else

now, this is the tricky part.
I want to activate my program which is already opened
and pass the value of the command$ line.

Condition: I can not use the forms caption to activate
it as it always displays a filename.

I have VB6.
0
Comment
Question by:idcjavateam
  • 2
3 Comments
 
LVL 1

Expert Comment

by:vignola
Comment Utility
put a title "mytitle" to your app and then modify app.title when it displays a filename to app.title=app.title & " " & filename.
Your second instance will have app.title and your previnstance app.title +filename.
Check app.previnstance at first line of code:
if app.previnstance then
dim firstapptitle as string
firstapptitle=app.title

Search with findwindow(myclassname,firstapptitle)
dim hwnd as long
hwnd=findwindow(classnamelong,firstapptitle)
search again:
hwnd=(classname,firstapptitle + "*.*")
the hwnd to previous instance handle, set focus
with setfocus(hwnd)
to and exit
0
 
LVL 6

Accepted Solution

by:
clifABB earned 120 total points
Comment Utility
Ok, here's how I would do it.

In your app, every time the caption changes, write that caption to the registry using SaveSetting().

When your all starts, check for previous instance and, if true, retrieve the caption from the registry and activate the app that way.

Also, pass the command string using the same method.  When your app is started, and there is a previous instance, write command$ to the registry.  In your app's main form's activate event, check for previous instance.  If false, retrieve command$ from the registry and use it.  Also, erease it immediately after retrieving it.

The code would look something like this:
Private Sub WriteCaption(sFilename As String)
  'This doesn't have to be a sub, but the save setting line
  'must go right after setting the form caption
  frmMain.Caption = sFilename
  SaveSetting "MyApp", "Activate", "Caption", sfilename
End Sub

Sub Main()
  Dim sCaption As String

  If App.PrevInstance Then
    SaveSetting "MyApp", "Activate", "Command", Command$
    sCaption = GetSetting("MyApp", "Activate", "Caption", "")
    AppActivate sCaption
  End If
End Sub

Private Sub Form_Activate ()
  Dim sCommand As String
  sCommand = GetSetting("MyApp", "Activate", "Command", "")
  If sCommand <> "" Then
    'Process Command
  End If
  DeleteSetting "MyApp", "Activate", "Command"
End Sub
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
Oops, the activate event should test for previous instance.
Chang the code to this:

Private Sub Form_Activate ()
  Dim sCommand As String
  If Not App.PrevInstance Then
    sCommand = GetSetting("MyApp", "Activate", "Command", "")
    If sCommand <> "" Then
      'Process Command
    End If
    DeleteSetting "MyApp", "Activate", "Command"
  End If
End Sub
0

Featured Post

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!

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 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

12 Experts available now in Live!

Get 1:1 Help Now