| | |
[req]How to move elements in the list box up and down using command button?
![]() |
•
•
Join Date: Mar 2007
Posts: 16
Reputation:
Solved Threads: 0
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.....
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.....
•
•
Join Date: Mar 2007
Posts: 59
Reputation:
Solved Threads: 5
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:
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:
VB Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Looking for List Box help, please! (VB.NET)
- How to move a selected item up/down in List Box? (C++)
- list box --php issue (PHP)
- My list box (Java)
- "Error in linking List box with the VB6.0 database" (Visual Basic 4 / 5 / 6)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: connect VB to OpenOffice(Calc) ?
- Next Thread: VB4 application issue
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





