How to Handle a Popup, Modal, Resizable form

主宰稳场 提交于 2020-07-22 09:29:18

问题


I need to have a popup form resizable for users whose screen is not as large as others - setting the form to Popup and Modal and BorderStyle Resizable has one major limitation - the code in the form that launches the popup now does not wait for the form to return.

So how does one wait for a form to be made invisible? I tried looping with sleep and doevents, but that makes the popup form not very responsive and chews up cpu cycles. I have tried setting the form.gotfocus event of the launching form but that does not trigger and which means I have to split the code that opened the popup form from the code that executes after the popup form is closed.

What is the best solution?

Thanks


回答1:


I have never had any problems with DoEvents / Sleep 50 loops. CPU load stays minimal and the form responsive.

With a very old computer, perhaps use Sleep 100.

Sample code:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub TestOpenForm()

    If FormOpenWait("frmPopup") Then
        MsgBox "Form was hidden.", vbInformation
    Else
        MsgBox "Form was closed.", vbInformation
    End If

End Sub

' Open fName, wait until it is
' - closed : return False
' - hidden : return True
Public Function FormOpenWait(fName As String, Optional sOpenArgs As String = "") As Boolean

    If IsFormLoaded(fName) Then DoCmd.Close acForm, fName, acSaveNo

    DoCmd.OpenForm FormName:=fName, OpenArgs:=sOpenArgs

    ' default: signal "closed"
    FormOpenWait = False

    ' Wait until form is closed or made invisible
    Do While IsFormLoaded(fName)
        If Not Forms(fName).Visible Then
            ' Signal "hidden"
            FormOpenWait = True
            Exit Do
        End If

        ' Wait 50ms without hogging the CPU
        DoEvents
        Sleep 50
    Loop

End Function

Public Function IsFormLoaded(fName As String) As Boolean
    IsFormLoaded = (SysCmd(acSysCmdGetObjectState, acForm, fName) And acObjStateOpen) > 0
End Function



回答2:


You can open the form with acDialog option:

DoCmd.OpenForm "MyFormName", WindowMode:=acDialog

It will wait until the form closed or hidden.




回答3:


The problem here is popup and modal forms don’t’ halt calling code.

But worse is your need to halt calling code. That requires a dialog form.

And more worse is a dialog form does NOT allow re-sizing.

You don’t want to confuse the 3 types of forms here.

Modal forms – they are different then popup forms, and very different from dialog forms.

And same goes for Popup forms. They are not dialog forms, and in fact they are also not model.

You also have to take into consideration if you Access application is set to use tabbed documents, or overlapping windows. (this will limit your choices again).

If you using tabbed interface, then you have to use a popup form if you want re-sizable ability – but this type of form will not halt calling code.

If you using overlapping windows, then I recommend a modal form. (But again it will not halt calling code, but will allow re-size).

While you “could” adopt some looping code in the calling form that “waits” for the second form to be dismissed, such loops are processor hogs and often cause a “poor” response in terms of mouse and typing.

So I would suggest that change your code approach. Have the calling form launch the form as popup (or modal if you not using the tabbed interface).

Then when you close the form, it calls some more code that you want to run in the calling form. This does mean you have to split out the code that you want to run after closing that 2nd form – and have the closing form call + run that code.

A “general” approach should avoid hard coding the forms names since then “many” routines and forms can call your second forms and that facilities re-use of such forms.

So try this: In the second form, create a module level form variable like this:

Option Compare Database
Option Explicit

Dim frmPrevious     As Form

In the forms on-open event of this form, GRAB the calling forms refeance like this:

Set frmPrevious = screen.ActiveForm

Now in the forms close event , do this:

frmPrevious.AfterClose

And in the calling form, create a public function called

AfterClose

So in the public function in the first form, you place the code that you want to run when you close the 2nd form. This is "less" ideal then nice procedural code that calls the 2nd form, waits and continues - but I think it still about the best choice.

And before you close, you can pass or “set” values to return to the calling form like:

frmPreviuos.SomePubicVar = me!LastNameSelected
frmPrevious!Company = me!CompanySelected
frmPrevious.AfterClose

In the above the first line sets a public variable in the calling form (if you want to do that and pass values back to module level vars in the first form). And the next line sets the value of a control in the calling form (if you want to pass values back to some controls on the calling form).

And the 3rd line executes the MyClose code in the calling form. And if you have some kind of “ok” or select button in that second form, then move the above code to behind that button – since you may not want such code running if the form has a cancel button (so the cancel button would simply close the form – but not call + run the above code in the first form). And hitting the X would also be assumed to be a cancel or exit. So your "save" or "ok" or "select" button would have the above code.



来源:https://stackoverflow.com/questions/46616036/how-to-handle-a-popup-modal-resizable-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!