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.



Recommended Answers

All 5 Replies

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?

i have d same problem.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.