[req]How to move elements in the list box up and down using command button?

Reply

Join Date: Mar 2007
Posts: 16
Reputation: Ved_TheOne is an unknown quantity at this point 
Solved Threads: 0
Ved_TheOne Ved_TheOne is offline Offline
Newbie Poster

[req]How to move elements in the list box up and down using command button?

 
0
  #1
Mar 24th, 2007
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


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

thnx in advance.....
Did  I  say  I  m  good?   Shit,  Im  a    Rembrandt
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 59
Reputation: PVBert is an unknown quantity at this point 
Solved Threads: 5
PVBert PVBert is offline Offline
Junior Poster in Training

Re: [req]How to move elements in the list box up and down using command button?

 
0
  #2
Mar 24th, 2007
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:

  1.  
  2. Option Explicit
  3. Private Sub Form_Load()
  4. 'set the multiselect property of the listbox control (LIST1) to FALSE!
  5.  
  6. 'set the following properties
  7. cmdAdd.Caption = "add"
  8. cmdRemove.Caption = "remove"
  9. cmdUp.Caption = "up"
  10. cmdDown.Caption = "down"
  11. End Sub
  12.  
  13. Private Sub cmdAdd_Click()
  14. 'add an item
  15. List1.AddItem "Item " & CStr(List1.ListCount + 1)
  16. End Sub
  17.  
  18. Private Sub cmdRemove_Click()
  19. 'remove an item
  20. If List1.ListIndex > -1 Then
  21. List1.RemoveItem List1.ListIndex
  22. End If
  23. End Sub
  24.  
  25. Private Sub cmdUp_Click()
  26. Dim sText As String
  27. Dim iIndex As Integer
  28. 'check: only proceed if there is a selected item
  29. If List1.SelCount = 1 Then
  30. 'index 0 is top item which can't be moved up!
  31. If List1.ListIndex = 0 Then Exit Sub
  32. 'save items text and items indexvalue
  33. sText = List1.List(List1.ListIndex)
  34. iIndex = List1.ListIndex
  35. 'remove item
  36. List1.RemoveItem List1.ListIndex
  37. 'place item back on new position
  38. List1.AddItem sText, iIndex - 1
  39. 'if you keep that item selected
  40. 'you can keep moving it by pressing cmdUp
  41. List1.Selected(iIndex - 1) = True
  42. End If
  43. End Sub
  44.  
  45. Private Sub cmdDown_Click()
  46. Dim sText As String
  47. Dim iIndex As Integer
  48. 'check: only proceed if there is a selected item
  49. If List1.SelCount = 1 Then
  50. 'check: last item can't be moved down
  51. If List1.ListCount - 1 = List1.ListIndex Then Exit Sub
  52. 'save items text and items indexvalue
  53. sText = List1.List(List1.ListIndex)
  54. iIndex = List1.ListIndex
  55. 'remove item
  56. List1.RemoveItem List1.ListIndex
  57. 'place item back on new position
  58. List1.AddItem sText, iIndex + 1
  59. 'if you keep that item selected
  60. 'you can keep moving it by pressing cmdDown
  61. List1.Selected(iIndex + 1) = True
  62. End If
  63. End Sub
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC