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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to copy listbox rows

Status
Not open for further replies.

k2a

Programmer
Jun 26, 2012
133
DE
I’ve 2 identical listboxes in a form. One of them gets filled by a search routine. Now, by clicking on one of the found item in the listbox, only this row should be copied the other identical listbox.

What would be a good solution to do that?

Thanks!
Klaus
 
The usual way to do this is a composite control called a "mover". You can find one ready to use in the FFC classes that ship with VFP. Take a look in _movers.vcx in the FFC folder.
 
In my form a mover is not practicable because both listboxes are combined. In my situation both listboxes must be separate.

Thanks for your idea anyway!
Klaus
 
What's your problem with the "combined" listboxes of the mover class? It's two listboxes in there, too. You need to modify code used to fill one of them with your search result, but isn't that possible?

Regardless of using that FFC class or not, it depends very much about how you populate the one listbox, using a cursor controlsource needs much different code to delete an item than a listbox filled by Additem().

I developed my own mover class and it has some properties on the container level to specify source and destination aliases. So it's based on cursors as rowsources. Moving items then is deleting them from one cursor and adding them to the other. That doesn't even need addressing the controls themselves. It's a bit tricky, using a cursor with a primary index I make use of both DELETE, SCATTER, GATHER and also RECALL, eg when moving a record forth and back again, the record needs to be RECALLed in the source cursor, because the primary index wouldn't allow adding it once more.

Bye, Olaf.
 
Here's a synopsis of the method I used:
First, create an array from the data; cursor, table, whatever. Then populate the first listbox from an array using Listbox.AddItem()
After the items have been selected and the user clicks the 'Move' button, I scan through the listbox starting with the last item working down to the first. That way I can delete each row in the original listbox as I move it to the new listbox.
After all the items are moved, you can grab the moved data using another array, cursor, whatever.
Here's a snippet for the 'Move' command button, where 'lbCustList' is the source listbox and 'lbCopyList' is the target:
Code:
PRIVATE indx

*... move selected items from left to right list box
WITH THIS.PARENT
   STORE 1 TO indx
   DO WHILE indx <= .lbCustList.LISTCOUNT
      IF .lbCustList.SELECTED(indx)
         .lbCopyList.ADDLISTITEM(.lbCustList.listitem(.lbCustList.IndexToItemID(indx)))
         .lbCustList.REMOVELISTITEM(.lbcustlist.IndexToItemID(Indx))
      ELSE
         indx = indx + 1
      ENDIF
   ENDDO
   *... refresh labels showing number of items in each listbox
   .lblCustListCount.Caption = 'Items: ' + ALLTRIM(STR(.lbCustList.LISTCOUNT))
   .lblCopyListCount.Caption = 'Items: ' + ALLTRIM(STR(.lbCopyList.LISTCOUNT))
   
ENDWITH


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top