Form document: Detect focus change between forms by macro

@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

@Villeroy , great job!

Thanks to all participants for a very interesting topic!
The attached example is based on the approach suggested by @Villeroy in this topic, using the FormController object. The macros from Module2 are from @Villeroy’s example.

After opening the FormFocus form, you need to click the Start button. After that, events of getting focus by top-level forms will be tracked. The ID field (if it exists in the form) is taken when the form gets focus.

Macros should also work in Standalone form. To process the subforms, you need to slightly modify the SetFormControllerListener macro code.

FormFocus.odb (36.2 KB)

1 Like

Seems to me this solution is exactly @ALO_Michael is looking for.

Have opened the example. There is set onFocus to every control, not only to the form. if there is any other macro planned for onFocus of a control it will be lost.
There must be a event for forms only. Navigationbar at the bottom of the forms register this event and change from form ton subform without any focus event of all of the controls …

Thank you @Ratslinger,

No, up to now there is no solution Please refer also to the reply from @RobertG.
What I do not understand: Jumping between the grey and both orange parts (Forms / TableControl), the content in the navigation bar changes showing the actual record number etc. So there must be an event signalizing such changes to LibreOffice.

Thank you very much @sokol92!

I started the .odb and pressed the start button. After that, the info (green fields) for form “ERP” are shown. That’s fine!
I’m not sure, that I used the START-button correctly; here it hides the navigation bar, which should not happen in the final solution. If I didn’t misunderstand the actual version, for the following issues I’d need enhancements:

  • Info after changes in form “Outgoing” are not shown so far.
  • Info after changes in form “Provider” are not shown so far.
  • Scrolling through the form “Provider” should not reset the START-Button.
  • The START-Button should not be necessary. This means, jumping through the three forms should trigger the macro code directly.
    Do you think, this is possible?

I will prepare a new version taking into account your wishes no later than tomorrow.

That event triggers “Before Record Change” and then “After Record Change”

Version 3.
FormFocus3.odb (37.2 KB)

3 Likes

Thanks a lot @sokol92, looks perfect! I have to understand your solution and to transform it into my application; this may take longer than tomorrow due to other obligations. After that, I’ll come back to you.
Thank you so far!

Thanks again,

I’m trying to understand the macros:

  • Am I right that in you3 FormFocus3.odb only the macros “Module3” are actual for your solution? If so, I’d delete Module1 and Module2 for my version.
  • In the code "Module3) I found Sub DocFormLoaded(oEvent) and Sub DocFormUnLoaded(oEvent), both are triggered in Menu > Extras > Customize > Event. The Form itself has Macros DocFormLoaded and FormUnLoaded (ref. picture):

    these makros I don’t find. Are you OK to delete both connections?

Hello!

Module3 uses the getParentObject function from Module2.

I apologize - these are traces of my experiments. You need to remove the handlers of these events using the appropriate button.

No need for any apology at all. I am so grateful for your amazing support!
I have reduced the database FormFocus 20230226 E.odb (16.8 KB) for my further research.

  • During testing, I have been a bit confused: After opening the form, the green field are empty (same in your version). Would it be possible, to set the focus on start to the first form (here “Provider”) and to disply information in green fields from there?
  • “Module3” lines 87…90 seem to be empty, but deleting the lines, seems to produce an error.

Thank you!

Let’s try changing one macro:

Sub DocFormLoaded(oEvent)
  Dim oDoc, oForm
  oDoc=oEvent.Source
  If oDoc.CurrentController.isFormDesignMode Then Exit Sub
  SetFormControllerListeners oDoc
  oForm=oDoc.DrawPage.Forms.getByName("Provider")
  FillMainForm oForm
  oDoc.CurrentController.getControl(oForm.getByName("fmtID")).setFocus
End Sub

For any listener in Basic, we must create macros to handle all possible events, regardless of whether we are going to respond to these events.