?
Solved

App.previnstance and command$

Posted on 1998-10-10
3
Medium Priority
?
461 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Expert Comment

by:vignola
ID: 1439191
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 240 total points
ID: 1439192
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
ID: 1439193
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

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