[Solved] Pass ListBox selected item to Textfield

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

[Solved] Pass ListBox selected item to Textfield

Post by mike phillips »

Back again - you know IF someone could tell me where to find all the properties and methods of this I MIGHT be able to sort it out on my own. As it is I am probably at around 40-50 Google searches and still stuck.

I need my Listbox (ListBox1) (single) selected item passed to a text box TextField1. The 'favourite' from Google seems to be

Code: Select all

item = dlg.getModel().getByName("ListBox1").getSelectedItem()

but it produces a 'Property or Method not found'. I have tried a multitude of other (guessed) expressions and none seem to work. Anyone please?
Last edited by mike phillips on Mon Mar 12, 2012 12:59 pm, edited 1 time in total.
OpenOffice 4 on Windows 7
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Pass ListBox selected item to Textfield

Post by JohnSUN-Pensioner »

Code: Select all

Sub ListBox1_OnChange(oEvent)
	oEvent.Source.Context.getControl("TextField1").setText(oEvent.Source.getText())
End Sub
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Pass ListBox selected item to Textfield

Post by mike phillips »

Are you telling me that the method

Code: Select all

dlg.getModel().getByName("ListBox1").
cannot be used to get a listbox selected item?
OpenOffice 4 on Windows 7
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Pass ListBox selected item to Textfield

Post by JohnSUN-Pensioner »

Why not? Use property Text:

Code: Select all

dlg.getModel().getByName("TextField1").Text = dlg.getModel().getByName("ListBox1").Text
Changing the contents of the control ListBox raises an event "Text modified". Why not use the information of the event?
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Pass ListBox selected item to Textfield

Post by mike phillips »

For the simple reason that that gives me 'Property or Method not found:Text'. That was my first go 5 hours ago!
OpenOffice 4 on Windows 7
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Pass ListBox selected item to Textfield

Post by JohnSUN-Pensioner »

Show the file, please
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Pass ListBox selected item to Textfield

Post by Villeroy »

Start some dialog with 3 controls ListBox1, TextField1, CommandButton1:

Code: Select all

Sub Main
oDlgDesc = GlobalScope.DialogLibraries.Standard.Dialog1
' generate "live" dialog
oDlgControl = CreateUnoDialog( oDlgDesc )
GlobalScope.BasicLibraries.loadLibrary("MRILib")
MRI oDlgControl

' display "live" dialog
iReturn = oDlgControl.execute()

REM returns 1 if an button of type "OK" had been clicked, else 0
msgbox "Return value of modal dialog: "& iReturn
End Sub
Except for the last line, the following code has been recorded by the MRI tool:

Code: Select all

Sub Snippet(Optional oInitialTarget As Object)
  Dim oControl As Object
  Dim sSelectedItem As String
  Dim oControl_2 As Object

  oControl = oInitialTarget.getControl("ListBox1")
  sSelectedItem = oControl.getSelectedItem()
  oControl_2 = oInitialTarget.getControl("TextField1")
  
  oControl_2.setText(sSelectedItem)
End Sub
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
JohnSUN-Pensioner
Volunteer
Posts: 876
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Pass ListBox selected item to Textfield

Post by JohnSUN-Pensioner »

+1
Attachments
Pass ListBox selected item to TextField.ods
(11.23 KiB) Downloaded 734 times
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.5, LibreOffice 6.4.4.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Pass ListBox selected item to Textfield

Post by mike phillips »

JohnS
Show the file, please
- not sure which 'file' you want me to show, but this is the ListBox1 click block.

Code: Select all

Private Sub ListBox1_Click()

d.getModel().getByName("ListBox1").EnableVisible = False 
d.getModel().getByName("TextField3").Text = "You have selected " &  " - is this correct?"
d.getModel().getByName("TextField2").EnableVisible = False
d.getModel().getByName("TextField3").EnableVisible = True
d.getModel().getByName("TextField1").Text = d.getModel().getByName("ListBox1").Text 
'invoiceperiod = d.getModel().getByName("ListBox1").getselected 
d.getModel().getByName("CommandButton1").EnableVisible = True 
d.getModel().getByName("CommandButton2").EnableVisible = True 

End Sub
Your line 5 is the failure. Regarding your latest ods - I am still trying to LEARN how to use one particular method - getModel and would like to see how to achieve this that way.

Villeroy - why do I have to change from getModel to getControl? Can it not be done by that method?

Arineckaig - where are you? I need you!!
OpenOffice 4 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Pass ListBox selected item to Textfield

Post by Villeroy »

mike phillips wrote:Arineckaig - where are you? I need you!!
Lame duck! That's what I told you when we first met.
Attachments
blah.odt
Dialog with list2text box copy
(12.13 KiB) Downloaded 524 times
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Pass ListBox selected item to Textfield

Post by mike phillips »

OK - it seems that so far we are beaten by this task, so I have to wait for someone who can do it.
Last edited by mike phillips on Thu Mar 08, 2012 12:04 pm, edited 1 time in total.
OpenOffice 4 on Windows 7
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Pass ListBox selected item to Textfield

Post by mike phillips »

All I need is the correct ASCII characters to go at the end of

Code: Select all

d.getModel().getByName("TextField1").Text = d.getModel().getByName("ListBox1").
Text/SelectedItem/getSelected/ItemSelectedItem() all do not work.
Last edited by mike phillips on Thu Mar 08, 2012 12:04 pm, edited 1 time in total.
OpenOffice 4 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Pass ListBox selected item to Textfield

Post by Villeroy »

mike phillips wrote:I have a reasonable amount of coding experience, having used C, Nokia QT and VBA.
So you have lots of alternatives, don't you?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Pass ListBox selected item to Textfield

Post by hanya »

Code: Select all

d.getModel().getByName("TextField1").Text = d.getModel().getByName("ListBox1").StringItemList(d.getModel().getByName("ListBox1").SelectedItems(0))
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Arineckaig
Volunteer
Posts: 828
Joined: Fri Nov 30, 2007 10:58 am
Location: Scotland

Re: Pass ListBox selected item to Textfield

Post by Arineckaig »

I see Hanya, who taught me all I know about dialogs, has supplied the answer that you require, but as I had already drafted this reply I may as well leave it.

I suspect the difficulty you are having is that the properties and methods for a list box in a dialog differ from those of a list box contained in a data form. The dialog list box is less developed and so far as I know does not have anything similar to '.GetSelectedItem....()' methods that are available to the normal list box form control.

To get the selected item from a dialog list box it is necessary first to read its '.SelectedItems()' property which is a sequence/array of pointers to the location in the list of the item(s) that have been selected. In the list box does not permit multi selections, the pointer will be supplied by the '.SelectedItems(0)' property. This pointer can then be used to read the '.StringItemList()' property of the list box which again is a sequence/array. Thus the code would be something along these lines.

Code: Select all

i = Dlg.getModel().getByName("ListBox1").SelectedItems(0) 
TargetTxt = Dlg.getModel().getByName("ListBox1").StringItemList(i)
When this issue has been resolved, it would help other users of the forum if you add the word - [Solved] - to the Subject line of your 1st post (edit button top right).
AOOo 4.1.5 & LO 6 on MS Windows 10 MySQL and HSQLDB
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Pass ListBox selected item to Textfield

Post by mike phillips »

Thanks for replying, both of you. At my level I would never have worked that out!

However, I have tried both constructs.

Hanya's

Code: Select all

Private Sub ListBox1_Click()
d = CreateUnoDialog( DialogLibraries.Standard.Dialog1)  'Load the existing Dialog called Dialog1
d.execute()
d.getModel().getByName("TextField2").EnableVisible = False
d.getModel().getByName("TextField1").EnableVisible = True

d.getModel().getByName("TextField1").Text = _ d.getModel().getByName("ListBox1").StringItemList(d.getModel().getByName("ListBox1").SelectedItems(0))  ' **

End Sub

produces 'Index out of defined range' (there are 4 items in Listbox1) at the starred line,

and Arineckaig

Code: Select all

Private Sub ListBox1_Click()

d = CreateUnoDialog( DialogLibraries.Standard.Dialog1)  'Load the existing Dialog called Dialog1

d.execute()
Dim i
Dim TargetTxt 
i = d.getModel().getByName("ListBox1").SelectedItems(0)  ' **
TargetTxt = d.getModel().getByName("ListBox1").StringItemList(i)

d.getModel().getByName("TextField1").Text =TargetTxt
End Sub

produces the same 'Index out of defined range' at the starred line

I have presumably missed a step or two? It would be very useful to know where I can find this information
I suspect the difficulty you are having is that the properties and methods for a list box in a dialog differ from those of a list box contained in a data form.
Last edited by mike phillips on Thu Mar 08, 2012 12:35 pm, edited 1 time in total.
OpenOffice 4 on Windows 7
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Pass ListBox selected item to Textfield

Post by Villeroy »

Of course the list box does provide method getSelectedItem(). But the model does not have such a thing. The model of a dialog control is what you see in the dialog editor. In edit mode there are no selectable data. Once you loaded the dilaog, all the "living controls" have window properties and data.
My little test document demostrates everything based on MRI recorded code. Just push the button on the document twice since I forgot to load the dialog library. It is loaded after the fist try raised an error.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Arineckaig
Volunteer
Posts: 828
Joined: Fri Nov 30, 2007 10:58 am
Location: Scotland

Re: Pass ListBox selected item to Textfield

Post by Arineckaig »

produces the same 'Index out of defined range' at the starred line
Probably not the cause, but just a thought you would get this error if nothing had been selected in the list box after the dialog had been executed.
It would be very useful to know where I can find this information
IMHO the MRI extension is the best means of getting this informatio. Hanya wrote the extension and Villeroy recommends it. Again IMHO you could not have two more reliable sources of information and advice.
When this issue has been resolved, it would help other users of the forum if you add the word - [Solved] - to the Subject line of your 1st post (edit button top right).
AOOo 4.1.5 & LO 6 on MS Windows 10 MySQL and HSQLDB
Arineckaig
Volunteer
Posts: 828
Joined: Fri Nov 30, 2007 10:58 am
Location: Scotland

Re: Pass ListBox selected item to Textfield

Post by Arineckaig »

Villeroy:
Of course the list box does provide method getSelectedItem(). But the model does not have such a thing.
My apologies. You are of course correct. I should have confined my comment to the model of the dialog. Your "blah.odt" says it all. I will limp back to my pond.
When this issue has been resolved, it would help other users of the forum if you add the word - [Solved] - to the Subject line of your 1st post (edit button top right).
AOOo 4.1.5 & LO 6 on MS Windows 10 MySQL and HSQLDB
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Pass ListBox selected item to Textfield

Post by Villeroy »

mike phillips downloaded xray which is just fine. But no helper tool nor API reference can help if you are a cargo cult coder.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Pass ListBox selected item to Textfield

Post by mike phillips »

Arineckaig wrote:IMHO the MRI extension is the best means of getting this information.
- indeed, and it was installed some time ago. However, I do not know how to use it and have asked for guidance elsewhere on this forum but there has been no answer.

The MRILib is there in My Macros MRILib/Module1. I have the 'add on' on the tools menu with a 'selection' option . Lots of Google searches has not produced any guide to use these - are there any? For example - I wish to look at the (dialog) listbox. How?

It would be useful if I knew how to establish whether I have populated my listbox correctly following your comment earlier.
OpenOffice 4 on Windows 7
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Pass ListBox selected item to Textfield

Post by kingfisher »

For MRI, you need to write these lines into your code. When you don't want to use them, include the leading apostrophe or REM at the beginning of each line. To use these lines you need to remove the leading apostrophes.

Code: Select all

'Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
'oMRI = CreateUnoService( "mytools.Mri" )
'oMRI.inspect( oStats ) ' REPLACE oStats with the object you wish to inspect
I have these in my Standard library, module 1 so that I don't have to load the MRI or Xray libraries ad hoc to use them. Thus, I do not require the first line of code mentioned above.

Code: Select all

Sub LoadingLibraries

BasicLibraries.LoadLibrary( "XrayTool" )
Globalscope.BasicLibraries.LoadLibrary( "MRILib" )

End Sub

Sub InspectDesktop

oMRI = CreateUnoService( "mytools.Mri" )'
oMRI.inspect StarDesktop
'Xray StarDesktop

End Sub

Sub InspectDocument

oMRI = CreateUnoService( "mytools.Mri" )
oMRI.inspect ThisComponent
'Xray ThisComponent

End Sub
Apache OpenOffice 4.1.9 on Linux
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Pass ListBox selected item to Textfield

Post by Villeroy »

kingfisher wrote:To use these lines you need to remove the leading apostrophes.

Code: Select all

'Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
'oMRI = CreateUnoService( "mytools.Mri" )
'oMRI.inspect( oStats ) ' REPLACE oStats with the object you wish to inspect
I have these in my Standard library, module 1 so that I don't have to load the MRI or Xray libraries ad hoc to use them. Thus, I do not require the first line of code mentioned above.
Once the library is loaded manually or by .LoadLibrary("MRILib") then you can use the existing Basic macro in that MRILib and simply call

Code: Select all

mri oStats
Sub mri in that library creates the UNO service and inspects the given object.

If you createUnoService and inspect on your own then you don't have to load the Basic library MRILib. The mytools.Mri UNO service is just the same as any other UNO service.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Pass ListBox selected item to Textfield

Post by kingfisher »

Thanks. I got those lines from someone else. Still learning. I'll alter my scripts.
Apache OpenOffice 4.1.9 on Linux
mike phillips
Posts: 118
Joined: Wed Feb 15, 2012 9:35 pm

Re: Pass ListBox selected item to Textfield

Post by mike phillips »

Thanks to outstanding assistance from a member here, some significant gaps in documentation have been filled regarding MRI use and all is sorted, so 'SOLVED'.
OpenOffice 4 on Windows 7
Post Reply