OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Get a substring from a ListBox

 
Post new topic   This topic is locked: you cannot edit posts or make replies.    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
gizmo
OOo Advocate
OOo Advocate


Joined: 13 Oct 2004
Posts: 248
Location: Everywhere

PostPosted: Mon Feb 01, 2010 4:02 am    Post subject: Get a substring from a ListBox Reply with quote

I have a ListBox filled with a SQL query:
SELECT CONCAT( ID+0, ' - ', Name ) FROM Animals

I do this because I want a list like this:
11 - Dog
12 - Cat
13 - Bird

In a macro I would like to get only the ID from this ListBox.
Only 11, 12 or 13 from the example above.
My code:
myform = thisComponent.Drawpage.Forms(0)
myvalue = myform.getbyname( "ListBoxName" ).getCurrentValue

How can I substring myvalue ?
I need only the first 2 chars.
Thank you in advance


Last edited by gizmo on Sun Mar 20, 2011 9:40 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2697
Location: Apeldoorn, Netherland

PostPosted: Mon Feb 01, 2010 4:54 am    Post subject: Reply with quote

Hello

You do not need a macro.
The query must also contain the value you need and that value is going in your field.

Make first a query and use that query in the list box

Activate the wizard for listbox on your toolbar.

SELECT CONCAT( ID+0, ' - ', Name )as "value" , "ID" FROM Animals

Make your queries in the GUI for queries.

Romke
Back to top
View user's profile Send private message
gizmo
OOo Advocate
OOo Advocate


Joined: 13 Oct 2004
Posts: 248
Location: Everywhere

PostPosted: Mon Feb 01, 2010 5:13 am    Post subject: Reply with quote

Hi Smile
Mmm... I don't understand what you mean.
I'm trying something as you wrote but using getCurrentValue I get always "ID - Name" not only "ID"...

BTW, the ID I get is used in another macro... to filter another table.

For example:
myform.ApplyFilter = True
myform.Filter = "IDanimal = " & myvalue
myform.Reload


RPG wrote:
Hello

You do not need a macro.
The query must also contain the value you need and that value is going in your field.

Make first a query and use that query in the list box

Activate the wizard for listbox on your toolbar.

SELECT CONCAT( ID+0, ' - ', Name )as "value" , "ID" FROM Animals

Make your queries in the GUI for queries.

Romke


Last edited by gizmo on Sun Mar 20, 2011 11:44 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2697
Location: Apeldoorn, Netherland

PostPosted: Mon Feb 01, 2010 6:57 am    Post subject: Reply with quote

Hello

I think you to make have a choice
a) study basic
use the left ,mid and instr function from basic
b) Learn working with OOo

I don't give the code.

Learn working with
a) dummy table
b) store a value in a dumy table
c) reload a form or sub form when you push a button


follow this thread, it is the same

Romke
Back to top
View user's profile Send private message
c2oncpr
General User
General User


Joined: 20 Aug 2011
Posts: 9

PostPosted: Tue Aug 23, 2011 7:04 pm    Post subject: Reply with quote

Gizmo, did you find a resolution to this delima? I'm trying to do exactly what you're doing, only my ListBox is Multi-select. Didn't look like your's was.

I understand the reason for your approach. Since OOo does not support Value and ID for multi-select listbox, you have to roll your own.

RPG, if you know something that's not obvious, would you mind sharing? That's why we're here, right?

My roadblock is I don't seem to be able to get the list box value at a certain position. Should be easy, not for me! SelectedItems gives me the list positions for the selected rows. But getSelectedItems and getItem give "no such method".

For example, the block below produces the selected items positions. Replacing SelectedItems with getSelectedItems or getItems gives the "no such method" error. This contradicts the documentation, unless I'm missing something, OOo 3.3.0


oForm = ThisComponent.DrawPage.Forms.getByIndex(0)
oMemList = oForm.getByName("ListMembers")
for each varItem in oMemList.SelectedItems
msgbox varItem
next
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2697
Location: Apeldoorn, Netherland

PostPosted: Wed Aug 24, 2011 2:03 am    Post subject: Reply with quote

Hello

Learn what you can do with SQL. I think use in your case a gridcontrol.
Forget macros for a long time special when you don't have study SQL and Forms.

Maybe this can help you a little
http://user.services.openoffice.org/en/forum/viewtopic.php?f=83&t=28235

Romke
Back to top
View user's profile Send private message
Arineckaig
OOo Advocate
OOo Advocate


Joined: 01 Mar 2004
Posts: 358

PostPosted: Wed Aug 24, 2011 6:00 am    Post subject: Reply with quote

Quote:
My roadblock is I don't seem to be able to get the list box value at a certain position. Should be easy, not for me! SelectedItems gives me the list positions for the selected rows. But getSelectedItems and getItem give "no such method".

To read multiple items selected in an unbound list box, it is necessary to access the 'view' of the list box. The .getSelectedItems() method of the view supplies a string array or sequence of the items that have been selected in the list box.
_________________
When this issue has been resolved, it helps other users of the forum if you add the word [Solved] to the Subject line of your 1st post (edit button top right).
OOo 3.4.1 and MySQL on MS Windows XP and Ubuntu
Back to top
View user's profile Send private message
c2oncpr
General User
General User


Joined: 20 Aug 2011
Posts: 9

PostPosted: Wed Aug 24, 2011 6:05 am    Post subject: Reply with quote

Quote:
The .getSelectedItems() method of the view supplies a string array or sequence of the items that have been selected in the list box


That's what I thought. But I'm getting "no such method". Can you see what I'm doing wrong?
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2697
Location: Apeldoorn, Netherland

PostPosted: Wed Aug 24, 2011 7:04 am    Post subject: Reply with quote

Hello

Learn to use Xray

Example
http://www.oooforum.org/forum/viewtopic.phtml?t=107999&highlight=selecteditems+sub

Ibelieve it is not part of the view but of the model

Romke
Back to top
View user's profile Send private message
Arineckaig
OOo Advocate
OOo Advocate


Joined: 01 Mar 2004
Posts: 358

PostPosted: Wed Aug 24, 2011 7:07 am    Post subject: Reply with quote

Quote:
oMemList = oForm.getByName("ListMembers")

That line gives you the 'model' of the list box, but you also require its 'view'. The model-view paradigm is an essential element for understanding the OOo API: the 'view' is obtained via the CurrentController of the containing form document or direct from the event source if an event of the list box triggers the macro.

As RPG advised try to avoid reliance on macros until you have gained an initial understanding of the API which differs fundamentally from VBA. As a start, I would suggest: this Guide or this site where the two PDF tutorials (OOo Basic Db Development and Forms & Dialogs) shown at the bottom of that site are a good introduction. The API is a steep but fascinating learning curve but also can readily be avoided as most projects in Base can be achieved without resort to macros.
_________________
When this issue has been resolved, it helps other users of the forum if you add the word [Solved] to the Subject line of your 1st post (edit button top right).
OOo 3.4.1 and MySQL on MS Windows XP and Ubuntu
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2697
Location: Apeldoorn, Netherland

PostPosted: Wed Aug 24, 2011 9:15 am    Post subject: Reply with quote

Hello

The view and model have both the properties /methode SelectedItems. Both give back an array but the content is different.


Code:
Sub Main(oEvent as object)
oListboxView= oEvent.source
print oListboxView.model.currentvalue
print oListboxView.SelectedItems(0)
print oListboxView.model.SelectedItems(0)
End Sub


Romke
Back to top
View user's profile Send private message
c2oncpr
General User
General User


Joined: 20 Aug 2011
Posts: 9

PostPosted: Wed Aug 24, 2011 9:33 am    Post subject: Reply with quote

The distinction between the model and view helps a great deal as I didn't realize it was something to be considered. Using the CurrentController is where I need to go, which in fact I did use in a different macro which was suggested in a different post.

RPG's example is slick, but I'm not sure will work in my immplementation. I'm trying to process the listbox selection when a button is pressed. In this case, the event source would be the button and not the listbox.

If I finish with a solution like Gizmo originally proposed, I'll post the code. But after taking a look at the suggested links, I may change to a "no macro", "ide designed" approach.

Thank you both for the followups.
Back to top
View user's profile Send private message
Arineckaig
OOo Advocate
OOo Advocate


Joined: 01 Mar 2004
Posts: 358

PostPosted: Wed Aug 24, 2011 2:15 pm    Post subject: Reply with quote

Quote:
The view and model have both the properties /methode SelectedItems. Both give back an array but the content is different.

The arrays are significantly different.

The SelectedItems property of the model comprises an array of the positions of the selected items whereas that property of the view comprises an array of the content of the selected items.

In other words the SelectedItems property of the list box model is the same as the SelectedItemsPos property of the list box view.

Later Edit: I quite overlooked that the CurrentValue property of the list box model equates with the SelectedItems property of the list box view.
_________________
When this issue has been resolved, it helps other users of the forum if you add the word [Solved] to the Subject line of your 1st post (edit button top right).
OOo 3.4.1 and MySQL on MS Windows XP and Ubuntu


Last edited by Arineckaig on Thu Aug 25, 2011 4:30 am; edited 1 time in total
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2697
Location: Apeldoorn, Netherland

PostPosted: Wed Aug 24, 2011 4:18 pm    Post subject: Reply with quote

Hello

This methode is working for me when you select data in a listbox and the listbox keeps still the focus when you click on a button.
The listbox is not bound to a field in a database.

It does only work when the list box has the focus.

Code:
REM  *****  BASIC  *****
'This show the selected content of the list box
Sub Main (oEvent as object)
dim oFormModel,oFormview,oListboxview,oListboxmodel ' Define the variables
dim y,z
oFormModel=oEvent.source.model.parent ' Go from a button to the form
oFormview=thiscomponent.currentcontroller.getFormController(oFormModel) 'Take the formview
oListboxview=oFormview.CurrentControl ' Take the current control in the form
' test if the current control is a listbox
if oListboxview.implementationname = "com.sun.star.comp.forms.OListBoxControl" then
   ' collect the data
   
      y=""
      for x=0 to ubound(oListboxview.selecteditems)
      y=y &  oListboxview.selecteditems(x)  & chr(13)
      next
      msgbox y
      
      'Now go to the model
      oListboxmodel=oListboxview.model
      y=""
      for x=0 to ubound(oListboxmodel.selecteditems)
      z=oListboxmodel.selecteditems(x)
      y=y & oListboxmodel.StringItemList(z)  & chr(13)
      next
      msgbox y
else
   print " The focus is not more on the listbox"
end if
End Sub


It show what you can do with the formview.
it is real fascinating.

Romke
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   This topic is locked: you cannot edit posts or make replies.    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group