Refresh a Form when a ListBox updated

My system info:

Version: 7.5.5.2 / LibreOffice Community
Build: caf8fe7424262805f223b9a233
Environment: CPU Threads: 4;OS: Windows 10.0 Build 19405
User Interface: UI render: Skia/Raster; VCL:win
Locale: en.GB (en_GB); UI: en-GB
Misc Calc: threaded
Database HSQLDB Embedded

I have attached a ‘filter’ database as an example. This was kindly provided by Villeroy in an earlier post.

In the Expense Form, the various entries chosen in the ListBoxes is reflected in the subform by clicking on the Apply Filter button. The buttons action is ‘Refresh Form’ (that’s the subform in this case).

I was wondering if the ‘After Update’ Event of the ListBoxes could have some code run (Macro?) that achieves the Refresh action. This would avoid having to manually refresh the subform using the button. Perhaps ‘After Update’ is not the appropriate place for this code but hopefully you get the idea.

I’ve done a few search’s and found examples pretty similar to what I want but they show code that’s either in German or is more complicated (eg for refreshing not just the parent form).

What I want is simple: each time a User selects a value from a dropdown list in a ListBox, the parent form is refreshed automatically (reloaded?). Thanks.

Edit: I’ve just looked again at the attached db and the Listboxes are on the main form (Filtering Form) and the button is on the subform (Filtered Form). So the code on the main form ListBoxes needs to refresh the subform, not the main form.

Expense.odb (24.8 KB)

There is a Listbox in a form, which connects to a table with only one row?

SUB Reload(oEvent AS OBJECT)
   oField = oEvent.Source.Model
   oForm = oField.Parent
   oField.Commit
   oForm.UpdateRow
   oForm.Reload
END SUB

Set this to the event “status changed” of the listbox. It will save the value of the listbox and will reload the form. In your example it is the subform, which has to be reloaded, but reloading the mainform will also reload the subform.

2 Likes

Thank you. I updated my post as you composed your reply. But I see that your suggestion should apply just the same. I will try it now.

That works perfectly. Marked as solution. Thanks.

Thank you for this. This almost works, but I’m surprised one thing doesn’t work.

Main Form
List Box 1
List Box 2
–Sub Form
–Table Grid

I attached your macro to List Box 1 → Changed
It does save the List Box 1 value and refresh the Table Grid which is in a Sub Form
But List Box 2 does not refresh! It is in the Main Form the same level as List Box 1.
How come?

List Box 2 content depends on the selection of List Box 1 and I have to put a refresh button for it to work.

But your Macro Refreshes the parent form why List Box 2 doesn’t get refreshed?!

Ok I made it work by adding also
oForm.getByName( “ListBox2” ).refresh

I would like to know why oForm.Reload is not enough?

In most cases a listbox will show the same content for all entries in a form. I could only guess: Refreshing all this listboxes every time a form will be reloaded is mostly useless traffic for processor.
Have a look at the navigation bar of a form. There are also “Form reload” an “Refresh Field”.

So what is the correct way to do this?
How can you send refresh command to oForm?
oForm.Refresh does not exist.

You could only reload a form.
You could only refresh a form control.
When reloading a form the form will be set to the first row. So it might be a good idea to save the number of the old row, reload the form and move to the row again.
Something like this:

    loRow = oForm.getRow
	oForm.reload
	IF loRow > 0 THEN
		oForm.absolute(loRow)
	ELSE
		oForm.moveToInsertRow
	END IF
1 Like

I want to achieve the same result as the button that refreshes.
How can I refresh the main form control with Macro?

Seem my English isn’t clear enough: A form could be reloaded. Reloading a form doesn’t refresh a form control. You have written the code for refreshing “ListBox2”. Which control do you else want to refresh?

I know. I understand.
I asked, please tell me how to refresh a form control.

I want to refresh the form control of the main form so that everything gets refreshed.
If I have 10 List Boxs, I want all of them get refreshed so I don’t write it 10 times.

You could put all the names of the listboxes in an array. Every listbox will need it’s separate refresh.
If cursor is in a subform and oForm is subform, oForm.Parent will be mainform.
Only other way: Close the whole form document and reopen it.

ThisDatabaseDocument.FormDocuments.getByName("myForm").close()
ThisDatabaseDocument.FormDocuments.getByName("myForm").open()

But we can put a button on the form and set the button Action to Refresh form.
How to do that in Macro?

You could try to get this behavior with .uno:Refresh.

sub refreshForm
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:Refresh", "", 0, Array())
end sub

Have written this also in German Base Guide.
But you have to set the position also after refreshing the form.