[Gambas-user] Listbox move items

timothy timothy.marshal-nichols at ...247...
Mon Mar 12 08:18:12 CET 2007


> -----Original Message-----
> From: gambas-user-bounces at lists.sourceforge.net [mailto:gambas-user-
> bounces at lists.sourceforge.net] On Behalf Of Leonardo Miliani
> Sent: Saturday, 10 March 2007 05:49 PM
> To: mailing list for gambas users
> Subject: Re: [Gambas-user] Listbox move items
> 
> Peter Mathijssen ha scritto:
> > Hi,
> >
> > I want to move items in a listbox up and down with the help of two
> buttons.
> > Has anyone an idea to do this?
> > I searched the internet, other basic languages for example code, but
can
> not
> > find anything.
> 
> Uhm... maybe you could try to use the ListView widget, that is more
> flexible than ListBox.
> 
> --
> Ciao.
> Leo.
> 

Moving items up and down a list box is not that complex. You simply
remove the item and then insert it in the required location. The ListBox
Add method has an optional index parameter you can use for this.

Some things you need to watch for are:
	Make sure there is something in the list box to move
	Not moving the first item up
	Not moving the last item down
	Make sure an item is selected (i.e. its index is not -1)
 
So here is the code you could place in a couple of buttons for a list
box called ListBoxSelected:

' Move a selected item up
PUBLIC SUB ButtonUp_Click()
  DIM tmp AS String
  DIM i AS Integer
  IF ListBoxSelected.Count <= 1 THEN RETURN
  IF ListBoxSelected.Index > 0 THEN
    tmp = ListBoxSelected.Text
    i = ListBoxSelected.Index
    ListBoxSelected.Remove(i)
    ListBoxSelected.Add(tmp, i - 1)
    ListBoxSelected[i - 1].Selected = TRUE
  END IF
END

' Move a selected item down
PUBLIC SUB ButtonDown_Click()
  DIM tmp AS String
  DIM i AS Integer
  IF ListBoxSelected.Count <= 1 OR ListBoxSelected.Index < 0 THEN RETURN
  IF ListBoxSelected.Index < (ListBoxSelected.Count - 1) THEN
    tmp = ListBoxSelected.Text
    i = ListBoxSelected.Index
    ListBoxSelected.Remove(i)
    ListBoxSelected.Add(tmp, i + 1)
    ListBoxSelected[i + 1].Selected = TRUE
  END IF
END


Moving items up and down a ListView is if anything slightly more
complex. This is because a ListView uses a key (not an index). Again
remove the item and store its info. Then add the item using the setting
the After optional parameter to the Add method. Here some code that will
do the job called ListViewselected:


PUBLIC SUB ButtonUp_Click()
  DIM itemKey AS String
  DIM itemText AS String
  DIM itemPicture AS Picture
  IF ListViewSelected.Current = NULL THEN RETURN 
  IF NOT ListViewSelected.MovePrevious() THEN
    itemKey = ListViewSelected.Item.Key
    itemText = ListViewSelected.Item.Text
    itemPicture = ListViewSelected.Item.Picture
    ListViewSelected.Remove(itemKey)
    ListViewSelected.Add(itemKey, itemText, itemPicture,
ListViewSelected.Current.Key)
  END IF 
END

PUBLIC SUB ButtonDown_Click()
  DIM itemCurrent AS String
  DIM itemBelow AS String
  DIM itemText AS String
  DIM itemPicture AS Picture
  IF ListViewSelected.Current = NULL THEN RETURN 
  IF NOT ListViewSelected.MoveNext() THEN
    itemBelow = ListViewSelected.Item.Key
    itemCurrent = ListViewSelected.Current.Key
    itemText = ListViewSelected.Current.Text
    itemPicture = ListViewSelected.Current.Picture
    ListViewSelected.Remove(itemCurrent)
    ListViewSelected.Add(itemCurrent, itemText, itemPicture, itemBelow)
    ListViewSelected[itemCurrent].Selected = TRUE
  END IF 
END

This same kind of logic will work for things like a ColumnView.

In all cases your list cannot be sorted as items would then spring back
to there original order ;-) 

Thanks

8-{)} Timothy Marshal-Nichols
<mailto: timothy.marshal-nichols at ...247...>







More information about the User mailing list