guaranteeing a form appears at the front of the zorder

Posted on 2005-04-07
Last Modified: 2010-05-02
here's the scenario:

1. i have  a standard exe that contains the main UI
2. i have an ActiveX exe that the standard exe calls sometimes to provide certain functionality
3. the ActiveX exe has a form in it which is displayed when the ActiveX exe is called.  this form is used to give the user status information

the problem:

- when my standard exe calls teh ActiveX exe - the form that the ActiveX exe pops up is ALWAYS underneath the standard exe.  I obviously need the form that the ActiveX exe displays.  how do i do that?

in the ActiveX EXE - when i display the form i use this code:

'putting up form that gives user progress and offers cancel button
            frmCancel.ZOrder 0
            intNumberOfFilesProcessed = 0
            frmCancel.lblStatus = "Recording information about the files you have selected"        
Question by:kenshaw
    LVL 19

    Expert Comment

    Your code should work (at least it does for me). Could you show us some more of it? Including call from standard exe
    LVL 7

    Accepted Solution

    Try to add this on AX exe form.


    Private Declare Function 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) As Long
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    Private Const HWND_TOP = 0
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long

    Private Sub Form_Load()
        SetWindowPos hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
        SetForegroundWindow hwnd
        SetActiveWindow hwnd
    End Sub
    LVL 32

    Expert Comment

    Why not just provide a method in the ActiveX EXE to retrieve the information?  Then you can use that information to display your own form.
    LVL 4

    Author Comment

    hmm... your code gives me an error on the Me.SetFocus call

    says its an invlid call
    LVL 4

    Author Comment

    the code in teh ActiveX EXE is called by 5 or 6 different parts of our main app.  we want all the code related to the tasks - including the GUI code in the ActiveX EXE.

    LVL 32

    Expert Comment

    There is only one way that I know of for a window to force its way to the front of other windows:

    "Force another window to the foreground, when SetForegroundWindow doesn't work, by attaching to its thread."
    LVL 15

    Expert Comment

    It also depends from where do you call ActiveX Exe;  e.g. if it is from MouseUp event in your main exe, it is possible that ActiveX form shows, but after that focus is returned to the original form/control in main app.  To check, set a breakpoint or msgbox immediately after  frmCancel.Show  line.
    LVL 5

    Expert Comment

    In ActiveX application you must be invoking the form from some function use vbModal like this

    Sub SomeFunction()
        Call Form1.Show(vbModal)
    End Sub

    In your application you can call it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Introduction While answering a recent question ( 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…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    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…

    779 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

    19 Experts available now in Live!

    Get 1:1 Help Now