hi, i want to write a code, which helps me to move up or move down the elements inside the list box at the click of command button

i have made use of 2 listbox's, one list box if filled with all possibles images, then at runtime user adds or remove the images as per his wish. these user defined list is stored in another listbox. now i got 2 command buttons "Move Up" and "Move down" which shuld move the items in this listbox.

thought it can be done with listbox.newindex but cannt do it:sad:


so any help will be appreciated:):):cheesy::cheesy:

thnx in advance.....

Ved_TheOne,

NewIndex isn't ment for that.
Just remove the item and add it at its new position.
Here's how I did it.

Place a listbox (list1) on a form
Place 4 command buttons on a form.
Name them: cmdUp, cmdDown, cmdAdd, cmdRemove
Place the following code in the General section:

Option Explicit
Private Sub Form_Load()
   'set the multiselect property of the listbox control (LIST1) to FALSE!
 
   'set the following properties
   cmdAdd.Caption = "add"
   cmdRemove.Caption = "remove"
   cmdUp.Caption = "up"
   cmdDown.Caption = "down"
End Sub
 
Private Sub cmdAdd_Click()
   'add an item
   List1.AddItem "Item " & CStr(List1.ListCount + 1)
End Sub
 
Private Sub cmdRemove_Click()
   'remove an item
   If List1.ListIndex > -1 Then
      List1.RemoveItem List1.ListIndex
   End If
End Sub
 
Private Sub cmdUp_Click()
   Dim sText As String
   Dim iIndex As Integer
   'check: only proceed if there is a selected item
   If List1.SelCount = 1 Then
      'index 0 is top item which can't be moved up!
      If List1.ListIndex = 0 Then Exit Sub
      'save items text and items indexvalue
      sText = List1.List(List1.ListIndex)
      iIndex = List1.ListIndex
      'remove item
      List1.RemoveItem List1.ListIndex
      'place item back on new position
      List1.AddItem sText, iIndex - 1
      'if you keep that item selected
      'you can keep moving it by pressing cmdUp
      List1.Selected(iIndex - 1) = True
   End If
End Sub
 
Private Sub cmdDown_Click()
   Dim sText As String
   Dim iIndex As Integer
   'check: only proceed if there is a selected item
   If List1.SelCount = 1 Then
      'check: last item can't be moved down
      If List1.ListCount - 1 = List1.ListIndex Then Exit Sub
      'save items text and items indexvalue
      sText = List1.List(List1.ListIndex)
      iIndex = List1.ListIndex
      'remove item
      List1.RemoveItem List1.ListIndex
      'place item back on new position
      List1.AddItem sText, iIndex + 1
      'if you keep that item selected
      'you can keep moving it by pressing cmdDown
      List1.Selected(iIndex + 1) = True
   End If
End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.