Addendum to changing defaults in Listboxes

Creating and using forms
Post Reply
gkick
Posts: 324
Joined: Wed Aug 07, 2019 5:24 pm
Location: Chile

Addendum to changing defaults in Listboxes

Post by gkick »

Hello,

How to change a property of a listbox control
Am evaluating a number of options to establish changing defaults for fields. The options so far looked at were setting the table fields default via sql which is no good if a form uses a listbox, an alternative would be cloning fields or records and yet another option was to sort of synchronise 2 listboxes using a filter table, but why use 2 if it possibly can be done with just one listbox.
I thought since there is a default selection property associated with listboxes it might be possible to change this property once the selection changes by attaching a short sub to the change event in order for the last selected value to stick until changed again. Not up to speed yet with coding but experimenting with cut paste modify whatever code snippets I come across.
Here is my latest attempt throwing up an error and I am also wondering if the space in the property name could be another problem.
Any hints will be appreciated, db in the attachment
Thks
Attachments
DefaultToListBox.odb
(13.53 KiB) Downloaded 131 times
oops.JPG
Libre Office 6.4.6 on Windows 10 HSQL 2.51 backend
UnklDonald418
Volunteer
Posts: 1548
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Addendum to changing defaults in Listboxes

Post by UnklDonald418 »

I am also wondering if the space in the property name could be another problem.
Yes, spaces, spelling and even capitalization are important when addressing the API.

Since you are using an event associated with the listbox named "country", finding the control is quite simple.
Once you have the control it is also simple to obtain the Boundfield.Value.
If you use MRI to inspect DefaultSelection you will see that it is not just a simple value so an assignment like

Code: Select all

oControl.DefaultSelection = oControl.Boundfield.Value 
won't work, but it does appear to be a one element array.
I don't think this is your final answer but the following does appear to at least temporarily change the value in DefaultSelection.

Code: Select all

Sub Testit (oEv as Object)
Dim BV(0) as Integer

'If Not Globalscope.BasicLibraries.isLibraryLoaded("MRILib") Then
'      Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
'End If
'oMRI = CreateUnoService( "mytools.Mri" )
REM  get the control from the event.
oControl= oEv.Source
'oMRI.inspect oControl
REM  store the value in an array
BV(0) = oControl.Boundfield.Value  
REM assign the array to DefaultSelection
oControl.DefaultSelection = BV   

End Sub
I included the MRI code but it is commented out. Remove the apostrophes from the MRI related lines to enable the MRI tool and you can inspect the oControl object each time.the macro executes.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
gkick
Posts: 324
Joined: Wed Aug 07, 2019 5:24 pm
Location: Chile

Re: Addendum to changing defaults in Listboxes

Post by gkick »

Thank you very much UnclDonald418. I need to study the MRI in more detail. At the moment the selected value is correct, however what I noticed when entering a default value manually within the control, base changes it to a string so 1 becomes "1" in the properties list. Anyway have some good pointers, very much appreciated !

cheers
Gerhard
Libre Office 6.4.6 on Windows 10 HSQL 2.51 backend
Post Reply