Arranging Listbox items 
Author Message
 Arranging Listbox items

Is there a way to move items in a listbox?

for example:

there are 5 items in a listbox
can i get the fourth item to swap indexes with the fifth?

thanks

Shawn



Sat, 09 Apr 2005 23:57:59 GMT  
 Arranging Listbox items
Hi Shawn,

Yes you can. This code moves the selected item in a listbox one position up:

----------------------------------------
Private Sub CommandButton1_Click()
Dim lNewIndex As Long

  'Check if selected item is
  'not the first in list
  If ListBox1.ListIndex <> 0 Then
    'Get new position:
    'Current listidex - 1
    lNewIndex = ListBox1.ListIndex - 1
    With ListBox1
      'Add new item on new position
      .AddItem , lNewIndex
      'add text of selected item at new position
      .List(lNewIndex) = ListBox1.List(ListBox1.ListIndex)
      'Remove old item
      .RemoveItem (ListBox1.ListIndex)
      'Select new position
      .ListIndex = lNewIndex
    End With
  Else
    MsgBox "First item, can't move up"
  End If

End Sub
----------------------------------------

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/


Quote:
> Is there a way to move items in a listbox?

> for example:

> there are 5 items in a listbox
> can i get the fourth item to swap indexes with the fifth?

> thanks

> Shawn



Sun, 10 Apr 2005 04:58:03 GMT  
 Arranging Listbox items
Hi Shawn

I think you have to copy, remove and add to desired position. Following
example makes the swapping between fourth and fitth positions:

'Copy 5th item
a = ListBox1.List(4)

'Remove 5th item
ListBox1.RemoveItem (4)

'Add 5th item in position 4
ListBox1.AddItem a, 3

/BosseH



Quote:
> Is there a way to move items in a listbox?

> for example:

> there are 5 items in a listbox
> can i get the fourth item to swap indexes with the fifth?

> thanks

> Shawn



Sun, 10 Apr 2005 04:38:05 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Drag and Drop method of re-arranging items in a ListBox

2. Arranging Listview Items.

3. Arrange items in datagrid

4. How to re-arrange list items using dragdrop?

5. arrange items in datagrid

6. How to re-arrange list items using dragdrop?

7. Move ListBox items to another ListBox

8. Listbox with single character item corrupts item?

9. populating vb.net listbox from selected items of another listbox

10. Listbox items: copy to another listbox, but randomize their order

11. HOW_DO_I: Give one item in a listbox a special color, without coloring all the listbox-items ?

12. Highlight Items in Listbox

 

 
Powered by phpBB® Forum Software