Modal dialogs

From HashVB
Jump to: navigation, search
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

No doubt, some of you have come accross a situation where you have a modal window open and something needs to show a non modal window. As there is a modal window open, you can't then show a non modal window without it raising an error.

If you are lucky, you may find the App.NonModalAllowed property which sounds like it will solve all the problems. Unfortunately, it isn't quite this simple...

App.NonModalAllowed will ONLY return false when working in an ActiveX/COM/MTS environment, and will always be true when in a standard EXE environment.

Luckily, when a window is shown modally, it disables the current window which we can detect.

This code looks for any disabled windows and returns true if any have that flag set. It does have the drawback that manually setting .Enabled to False will also trigger it.

'This is a hack and relies on windows being disabled whan showing another modally
Public Function ModalWindowOpen()
Dim Form As Form
  For Each Form In Forms
    If IsWindowEnabled(Form.hwnd) = 0 Then
      ModalWindowOpen = True
      Exit For
    End If
End Function