Form document: Detect focus change between forms by macro

A BASE Form document has several forms, as well SingleForms as TableControls (continuous forms). I can jump through these elements e.g. by mouse click: The focus changes; LibreOffice recognizes these changes and displays for example the new record number.
I need to detect the change of focus by macro. The TableControl provides an event “when focus is received” that I can use to detect the change.
What is the alternative for the SingleForm? (EXPERT # 2.490)

IMPOV only controls, like a table Control or a Text Box, but not forms, provide the event “When receiving focus”
WRF
But the question is: What do you want to achieve?

Isn’t there a possibitity to get this information by a listener, depending on the position of the view cursor. When changing data of a form with subform and clicking on the background of the form document the data of the form will be saved, because cursor is leaving the form - not a special control of the form.

There are certain fields like “id”, “created” etc. in all forms; I’d like to collect / display this information without subordination or modification of data bindings.
My question: What means IMPOV in your text:

In My Point Of View

Yesterday, I didn’t even know that there are Listeners. In Andrew Pitonyak’s “BASIC-Makros für OpenOffice und LibreOffice”, I found a Macro DisplayAvailableEvents (Listing 289) and added this to a BASE form
Listener.odb (12.8 KB) (ref. attached .odb).
OnFocus is available for WRITER-documents, so should be also for BASE-Forms!?
My be, someone could provide some more detail how to use the lsitener for detection?

I could add a event with following code - without visible result:

DIM oEvent AS NEW com.sun.star.script.ScriptEventDescriptor
oEvent.ListenerType = "com.sun.star.awt.FocusEvent"
oEvent.EventMethod = "focusGained"
oEvent.ScriptType = "StarBasic"
oEvent.ScriptCode = "vnd.sun.star.script:Standard.Module1.MyMacro"
oForm.registerScriptEvent(0, oEvent)

Could see it with xray through oForm.getScriptEvents(0), but doesn’t work.

Sorry, but i don’t understand your goal.
It’s possible to extract data from a form or multiple forms and subforms by code, you also have access to the underlying database. No subordination or modification of data bindings<.
Please upload a sample .odb file and explain which datas you want to see, and where you want to use them later. Certain fields in certain forms are too uncertain to help you.

I will attach a German example - no problem at this moment, because all members of this thread will understand German:
FormularfelderUnsichtbar.odb (23.9 KB)
Open the form “Eingang”. Click with mouse to subform or to mainform. Label and date in the field for the date will change to mainform or subform. This is how it should work. But it will only work well if all fields of mainform are linked to the macro “SichtbarUnsichtbar” when receiving focus. And if there are many fields in the mainform you have to add the code to all fields.
Second form isn’t connected to any macro. Start macro “EventFormstart” instead. The event will be added (could see this by xray), but it won’t do anything …

Thank you!

Let’s assume a “top-form” having a “headline” with a text-field and a numeric field. Within this “top-form” there are two more “form1” (actual record ID = 1) and "form2 (actual record ID = 2) ", both type single form, independent from each other. Furthermore there is a “grid3” (actual record ID = 3) , type table control, connected to “form2” (although this does not matter). “form1” and “form2” and “grid3” each have a name and an ID-field.
Jumping through these forms by mouse clicking, I’d like the provide within the “headline”:

  • The name in the textfield,
    and
  • the ID (e.g. 1 or 2 or 3 depending on the focused form) in the numeric one.
    For “grid3” the event “focus gained” works fine. For the single forms “form1” and “form2” it seems, that the listener is needed. How to set up the listener for this purpose (inform about focus changes of single forms) correctly?

I won’t be able to learn German in the next few days. :confused:
I add the event listener a little differently:

    oEvent.ListenerType = "com.sun.star.awt.XFocusListener"
	oEvent.EventMethod = "focusGained"
    oEvent.ScriptType = "Script"
    oEvent.ScriptCode = "vnd.sun.star.script:Standard.Module1.TestEvent?language=Basic&location=document"

For standalone forms this works.

Please provide a sample .odb file.
It is not possible, that TableControl “grid3” bound to form2 displays another ID then the single ID-field of form2.

@sokol92 Have tried your solution. The listener works, but it sets focusGained to the grid control of the subform - don’t know how it is set to this special control. It doesn’t set a FocusListener to a form (mainform, subform).

But a form (unlike controls) does not support a listener com.sun.star.awt.XFocusListener.
Or am I misunderstanding something?

@RobertG
oForm.registerScriptEvent(nIndex, oEvent)will be set to the control having index nIndex on the form.

… and to listen to the events of the (main) form itself, you need to use the same method of the Forms object.

I’d appreciate to focus on the recognigition of “focus gained” for single forms …

  • "form1", based on Table1, shows
    • the record ID1 = 1,
    • text = first (two columns)
  • "form2" based on Table2, shows
    • the record ID2 = 2,
    • text = second (two columns)
  • "grid3", based on Table3, shows
    • the record ID3 = 3,
    • text = second → third,
    • fKey2 (foreignKey > Table2) = 2 (three columns)

Link between “form2” == “grid3” is ID2 == fKey2

Means, if grid3 reflects table3 it must be on form3.

Attached an odb, FormFocus.odb (34.2 KB) based on Robert’s version, partially transformed to English.:

  • Please refer only to Form “FormFocus”!
  • Open this normally, in the top area there are green field the “headline”, both fields shall be filled by macro whenever the forcus between the following forms has changed. Underneath there are:
    • a single form “provider” (grey)
    • a single form “ERP” (orange) and underneath
      • a table control “Outgoing” (dark orange).
        Hope, this makes it understandable now. The .odb could be used for an example (macro) for the listener; a message box “Focus changed” for the single forms should be sufficient for the beginning.

The FormController has its own focus events and a lot of interesting properties helping to write simple macros analogue to the document controller’s dispatch framework.
Biblio_Form_Dispatch.odt (23.8 KB)

4 Likes