Keeping Child of MDI on top of all forms

Is there a way to keep a child of an MDI on top of all other forms while it is open?  I don't think you can make them modal.  

Thanks a bunch.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You may the following procedure to do this. Note that this procedure 2 arguments: Form Name and a boolean value to specify whether you want the form on top or not.

Declare Function SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal dwX As Long, ByVal dwY As Long, ByVal dwCX As Long, ByVal dwCY As Long, ByVal wFlags As Long) As Long

Public Sub FormOnTop(argForm As Form, ByVal bTopmost As Boolean)

    Const SWP_NOSIZE = &H1
    Const SWP_NOMOVE = &H2
    Const HWND_TOPMOST = -1
    Const HWND_NOTOPMOST = -2

    Dim dwRet As Long, dwPos As Long

    If bTopmost Then
        dwPos = HWND_TOPMOST
        dwPos = HWND_NOTOPMOST
    End If

    dwRet = SetWindowPos(argForm.hWnd, dwPos, 0&, 0&, 0&, 0&, SWP_NOSIZE Or SWP_NOMOVE)

End Sub

' Now you can call this procedure to bring the form frmChild on top as follows:

Private Sub Commmand1_Click()

    FormOnTop frmChild, True

End Sub


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If you don't have any control or all the controls are disable on the form, then just put this piece of code

Private Sub Form_LostFocus()
End Sub

If you have controls on the forms then the lostfocus event of the form won't triggerd insted the lostfocus event of the control is triggered. So place the setfocus on the lostfocus event of all the control, it should work just fine.

If you want a global handler, then you have to do subclassing for the form.
jogmenAuthor Commented:
Neither of these solutions seem to work.

1.  The above code does not seem to work when choosing another child form.

2.  The setfocus event does not kick off when choosing another child form.

Any ideas?
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!


In my answer I forgot to mention that using FormOnTop function (that I previously gave you) will keep the child form on top even when you click on the other form. The trick is to use that function as follows:

Private Sub Form_Load()
    ' Put the form topmost
    FormOnTop Me, True

    ' Now specify No Top argument in the next line which will allow you to bring the other child forms on top
    FormOnTop Me, False
End sub

Hope this would help.
download the example from

In the example it is always the form1 which remains on top always.
jogmenAuthor Commented:

The function provided does not keep a child form on top if i call it in my child form upon load then click on another open child form that form goes on top.

It doesn't really appear to do anything.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.