[Solved] How to get the selected item of a listbox?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Willem9
Posts: 22
Joined: Tue Jul 14, 2009 9:44 pm

[Solved] How to get the selected item of a listbox?

Post by Willem9 »

Hello,

In a dialogwindow I made a listbox. The Items in the list are the 7 weekdays monday to sunday. In a worksheet I placed a button. A click on that button opens the Dialogwindow and the week day selection in the listbox can be made.

Can anyone help me to find out how to use the listbox selection? How and where can I see wich item was selected afther selection and closing the listbox Dialog window? Can I assign a worksheet cel to the selcted item in a listbox or is there another way?

Friendly Regards,
Willem
Last edited by Willem9 on Fri Jul 17, 2009 3:39 pm, edited 1 time in total.
OOo 3.1.X on MS Windows Vista
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to get the selected item of a listbox?

Post by Villeroy »

Why that complicated? This can be done without button, dialog and code.
Put your weekdays somewhere in a list and call menu:Data>Validity... Type:Cell Range, specify the range. The active cell gets a listbox to pick the day from. When you copy the cell, you copy the validation list with it.
See example in http://user.services.openoffice.org/en/ ... 690#p68690
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
Willem9
Posts: 22
Joined: Tue Jul 14, 2009 9:44 pm

Re: How to get the selected item of a listbox?

Post by Willem9 »

Dear Villeroy,
Thank you for your reply.
Your example in Viewtopic.php?t=14601 seems to be one possibility to make a choice from a list. However, for me this example is also rather complicated. I have to little experience with Ooo-macro's to know how to handle with that.
What I want is a Dialogsheet with a listbox that I can call up at any moment out of any worksheet with a button or with a keynote. I have made that befor in Excel-VBA. Now I should like to switch over to OpenOffice.org./calc what is not so easy.
I know how to make the Dialogsheet with the Listbox on it but my problem is how or where can I see wich Item was selected in the listbox? Depending on that selection the program has to continue with an assigned macro. (My example with the weekdays could also be replaced with a list of colors or any other choice list.)

If you understand what I mean, if you know how to connect a selected item in the listbox to a worksheetcell or how to know wich item in the listbox was selected, please tell me in explanation as simple as possible or with an example

Friendly regards
Willem
OOo 3.1.X on MS Windows Vista
FJCC
Moderator
Posts: 9563
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to get the selected item of a listbox?

Post by FJCC »

Here is some code that runs a dialog and puts the item chosen from the listbox in the dialog in cell F1 of Sheet1. You have to know the name of the listbox. You can find that by going to the dialog design tab, clicking on the list box to select it, right clicking and selecting Properties, and looking on the General tab.

Code: Select all

DialogLibraries.LoadLibrary( "Standard" )  'Load the standard Library
oDialog1 = CreateUnoDialog( DialogLibraries.Standard.Dialog1 )  'Load the existing Dialog called Dialog1
ListBox = oDialog1.getControl("ListBox1")
oDialog1.execute()
Item = ListBox.SelectedItem

Rem Put the chosen string into a cell
Doc = ThisComponent
Sheets = Doc.Sheets
Sheet = Sheets.getByName("Sheet1")
Cell = Sheet.getCellRangeByName("F1")
Cell.String = Item
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to get the selected item of a listbox?

Post by Villeroy »

There is absolutely no macro involved. The spreadsheet provides everything you need to get the job done in a visual manner. Spreadsheets provide a simplified, visual programming language for non-programmers. This is why I suggested this way. Personally, I would not use a spreadsheet just to write my own functionality on top of it. There are far better tools which allow you to write your own applications without a 350 Megabyte tankship on your back.

The file I attached in the other topic has some stupid errors. Apart from a wrong formula in Sheet2.D1, it is more complex than necessary. I'll replace it with a better version. In fact, it just demonstrates how to use the validation feature with weekday names.
This is the simplified and more useful version: weeknum2.ods
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
Willem9
Posts: 22
Joined: Tue Jul 14, 2009 9:44 pm

Re: How to get the selected item of a listbox?

Post by Willem9 »

Dear FJCC

Yes !!! This is exactly the code where I was looking for. Thank you very much for helping me. In particular the line “item=listbox.selected” was my (oh so simple) problem. Your reply is very helpful for me.
To learn more about macro's in OOo I ordered last week the book “OpenOffice Macros Explained” from author “Andrew Pitonyak” For the delivery I have to wait about 4 to 5 weeks. Perhaps you know this book.

Best regards,
Willem
OOo 3.1.X on MS Windows Vista
User avatar
TheGurkha
Volunteer
Posts: 6482
Joined: Thu Mar 13, 2008 12:13 pm
Location: North Wales, UK.

Re: [Solved] How to get the selected item of a listbox?

Post by TheGurkha »

Ubuntu 14.10 Utopic Unicorn, LibreOffice Version: 4.3.3.2
Gurkha Welfare Trust
Post Reply