Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Listbox Data

Status
Not open for further replies.

Gary Sutherland

Programmer
Apr 18, 2012
33
GB
I'm trying to get someone else's VFP9 program working. It displays a form containing a ListBox control. This is populated programmatically with information read from a table.

The Moverbar lets the user change the order that the items are displayed in and a command button writes them back to the table.

The problem is that it isn't saving them in the order they're displayed in - but back in the original order.

I've stepped through the save routine and can see that it's looping through each listitem in order and that the data is still held in the ListBox in ListItem number order. So, if the data is displayedbin order 1,2,4,3 it is still held in the list as 1,2,3 4.

I need a way of accessing each item in the ListBox in its displayed order. I can't see any obvious way of doing this.

Any ideas?
 
The listbox control has two properties that tell you the order of the items. They are both arrays. The List property is an array that contains the items in the order in which they are displayed. The ListItem property is an array that contains the items in the order of the List IDs. The List ID is relevant if you used the AddListItem method to populate the control.

So you should be able to determine the order in which the items are displayed by looping through the List array. It is then up to you to write the items back to the underlying table in that order (if that's what you want). The control doesn't do that automatically.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
From the top of my head I just know the moverbar property allows using the listbox control for sorting and in that case a control source alias is not useful, as only an array is sorted as visually displayed. So I think Mike is correct, if not, you should change the rowsourcetype to array and bind to an array that is guaranteed to be ordered as displayed. If the data is added by Additem() it's also in the listbox array, as Mike says the List is the array, I'm not sure, but would trust him.

Chriss
 
Thanks guys, I'll add some debugging code into the save method and have a look at the List array contents.

The customer's paying me by the hour so all is good :)
 
That's the sort of customer I like, Gary.

Regarding the Save method, I'm assuming that you have the RowSourceType set to 0, given that you are using Moverbars. In that case, keep in mind that there is no connection between the list box and the underlying table. As far as the list box is concerned, it just contains a bunch of text strings. So if the user changes the order of those strings, that change won't work their way back into the table. It will be up to you to save the change in your Save method.

Be sure to report back when you have made some progress.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Yes, RowSource is 0 and it's using the AddItem method to populate the ListBox.

Switching over from ListItem() to List() has worked like a charm. Thanks for the help.

Gary
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top