original question has been edited
How can a form open another form, enter a waiting loop, wait until the second form is closed, and then resume program execution after the waiting loop.
The aim is to enter a new record in a table and then populate/update the listbox of a calling form with the new entered ID. The “how to enter new value in list” case.
The incentive is that porting BASIC code to Python does not work.
In the Python sample code a restriction is added to the loop, otherwise LibO remained idle.
Unwanted:The form does not open before the loop is entered, but after the loop is finished.
CONCLUSION: use a dialog
import uno, unohelper
import time
from com.sun.star.util import XCloseListener
from com.sun.star.frame import FrameActionEvent
from com.sun.star.lang import EventObject
from com.sun.star.awt.PushButtonType import OK as PUSH_OK
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
FORMNAME = "frmtest"
DBNAME = "test"
# dialog is called from a form with a listbox that needs a new value
def dialog():
waitForSavedForm(FORMNAME, DBNAME)
# after waiting, execution resumes here, because listener set AB.waitForClosing to false
# closing of the form is by a macro,
# update of listbox boundfield to happen here, because ID now known
msgbox("done")
def waitForSavedForm(sFormName, dbName):
sfa = smgr.createInstance("com.sun.star.sdb.DatabaseContext")
oDataSource = sfa.getByName(dbName)
oData = oDataSource.DatabaseDocument.getFormDocuments().getByName(sFormName).open()
AB=CloseListener()
oData.addCloseListener(AB)
i = 0
while AB.waitForClosing and i<5:
time.sleep( 1)
i+= 1
class CloseListener(unohelper.Base, XCloseListener):
def __init__(self):
self.waitForClosing = True
def notifyClosing(self, evt: FrameActionEvent):
self.waitForClosing = False
msgbox("notifyClosing")
def queryClosing(self,evt: FrameActionEvent, ownership=True):
#msgbox("qC")
return
def disposing(self, evt: EventObject):
pass
def msgbox(message, title='LibreOffice', buttons=PUSH_OK, type_msg='messagebox'):
toolkit = smgr.createInstance('com.sun.star.awt.Toolkit')
parent = toolkit.getDesktopWindow()
mb = toolkit.createMessageBox(parent, type_msg, buttons, title, str(message))
return mb.execute()
In basic it looks like
Sub frmroot_omschrijving_leave(oEvent As Object)
oFormCalling = thiscomponent.getdrawpage.getForms.getByName("MainForm")
idlink = oFormCalling.getColumns.getByName("IDinvoice").getInt()
if oFormCalling.IsNew() and idlink = 1 then
oForm2 = ThisDatabaseDocument.FormDocuments.getByName("frminvoice-entry").open()
call DialogMode()
' after oForm2 is closed, the listbox can be filled with the ID of new added record
oFormCalling.getByName("listbox").boundfield.updateInt(idlink)
oFormCalling.getByName("listbox").refresh
End If
End Sub
'====================================================
Sub Form2Close_notifyClosing(ev)
bWaitForClosing = False ' terminates "endless loop"
End Sub
Sub Form2Close_disposing(ev)
' obtain new entered ID from the form and store in idlink
End Sub
Sub Form2Close_queryClosing(ev, ownership)
End Sub
'====================================================
sub DialogMode()
Dim oCloseListener as object
'oForm2 is entry form, execution is halted until form is closed
bWaitForClosing = True
oCloseListener = CreateUnoListener("Form2Close_" , "com.sun.star.util.XCloseListener")
oForm2.addCloseListener(oCloseListener)
Do
wait 500
loop until bWaitForClosing = False
end Sub