I have program where multiple forms remaine open at a time. I dont want user to move away from last opened form unless he closes that form. i.e last opened form should not lose focus by clicking on other forms unless active form is closed. How it can be done.



Use the following -

Form1.Show 1 'Or use vbModal - Shows modal, no other forms can be accessed unless form 1 is closed.


i used form1.show 1 , but getting error " MDI child forms cannot be shown modally.

any other option

You cant show a MDI child as vbModal... you need to use API to keep the "screen on top"

Add the following in a module -

Option Explicit

Private Const GWL_HWNDPARENT = (-8)

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, _
                                                                            ByVal nIndex As Long, _
                                                                            ByVal wNewLong As Long) As Long

Public Sub KeepScreenVisible(frm As Form)
    'if the form is to stay on top, frm.hwnd will get the name of the form
    Dim KeepScreenOnTop As Long
    KeepScreenOnTop = SetWindowLong(frm.hwnd, GWL_HWNDPARENT, [hl]frmMain[/hl].hwnd)
End Sub

In your form -

KeepScreenVisible Me

You can also simulate vbModal by setting the other form(s) to Enabled = False

If more than one form, run a loop to disable all other except the one you are working on.:)

i have d same problem. when i create d module, there have error
"hndw is not a member". how to declare hndw to be member?

Edited 5 Years Ago by shonen: n/a

i have d same problem.

If you have the same problem, read this thread. It's marked solved so the solution has been found.

This question has already been answered. Start a new discussion instead.