How to Emulate a Modal Form in VB

Showing Drop-Down Forms from Modal Forms.

One problem with this technique is that you cannot use it directly from a form shown with modally in VB, otherwise you get the error message "Can't show non-modal form when modal form is displayed". There 'must' be a workaround for this problem by which you can emulate showing the form modally.
Can anybody help me out with this problem ?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

fnizetConnect With a Mentor Commented:
You can disable the calling form.
Let's say MainForm calls ModalForm (which is not modal at all), you'll do
MainForm.enabled = false

Then in ModalForm, you can do to call your DropDownForm :
ModalForm.enabled = false

When you close your "modal-est" form, you just enable the calling form. Let's say you have a button to close the DropDownForm :
Sub CloseDropDown_Click()
    ModalForm.enabled = true
End Sub

To work properly, you may need to define 2 functions, one to call a modal form, the other to return from a modal form. You need to manage an array of the forms you locked, because several forms may need to be locked :

Dim lockedForms(1, 3)

Sub callModal ( calledForm as Form, caller as Form)
for each f in Forms
  if (f.hwnd<>calledForm.hwnd) ' prevent from self locking in case called form is already loaded
     And (f.enabled) then
    f.enabled = false
    i = Ubound(lockedForms(1))
    Redim Preserve lockedForms(i+1, 3)
    lockedForms(i, 1) = calledForm.hwnd 'save ID of calledForm, prevents unlocking of previously lock forms
    lockedForms(i, 2) = f.hwnd 'save ID of locked form
    lockedForms(i, 3) = caller.hwnd ' just to know which form to unlock
  end if

End Sub

Sub unlockForms(modalForm as Form)
  Dim formToShow As Form
  n = Ubound(lockedForms(1))
  do while (n>0 and lockedForms(n, 1) = modalForm.hwnd)
    for each f in Forms
      if f.hwnd = lockedForms(n, 3) then Set formToShow = f
      if f.hwnd = lockedForms(n, 2) then
        f.enabled = true
        exit for
      end if
    n = n - 1

  Redim Preserve lockedForms(n+1, 3)
End Sub

I have not tested this code, but the idea is here.
TemujinAuthor Commented:
your first method worked fine in my project.

Thank you very much mr fnizet.
you're welcome and thanks for the A grade.
All Courses

From novice to tech pro — start learning today.