[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Use spin button to move items in listbox up and down

Posted on 2008-10-09
2
Medium Priority
?
1,230 Views
Last Modified: 2013-12-26
I have a list box with a numerous items. I want to put a spinbutton beside it so that the user can select and item to move up and down in the list. As they move it I want to adjust the data on the excel sheet accordingly. I know how to move the data. but i don't know how to link the spinbutton and control how to move the data as it is pressed up or down. Thanks
0
Comment
Question by:tchristie33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
irudyk earned 1000 total points
ID: 22687610
Try using the following code.  Change SpinButton1 to the name of the name of your spin button control and change ListBox1 to the name of your list box control.
Private Sub SpinButton1_SpinDown()
Call MoveListItem(True)
End Sub
 
Private Sub SpinButton1_SpinUp()
Call MoveListItem(False)
End Sub
 
Function MoveListItem(blnShiftDown As Boolean)
 
If ListBox1.ListIndex < 0 Or _
ListBox1.ListIndex = 0 And Not blnShiftDown Or _
ListBox1.ListIndex = ListBox1.ListCount - 1 And blnShiftDown Then Exit Function
 
Dim i As Integer
Dim strListItems() As String
ReDim strListItems(ListBox1.ListCount - 1)
 
For i = 0 To ListBox1.ListIndex - IIf(blnShiftDown, 1, 2)
    strListItems(i) = ListBox1.List(i)
Next i
strListItems(i) = ListBox1.List(ListBox1.ListIndex + IIf(blnShiftDown, 1, 0))
strListItems(i + 1) = ListBox1.List(ListBox1.ListIndex - IIf(blnShiftDown, 0, 1))
 
For i = ListBox1.ListIndex + IIf(blnShiftDown, 2, 1) To ListBox1.ListCount - 1
    strListItems(i) = ListBox1.List(i)
Next i
 
ListBox1.List = strListItems
 
End Function

Open in new window

0
 
LVL 22

Assisted Solution

by:danaseaman
danaseaman earned 1000 total points
ID: 22692279
You can swap List items using just a temp string. No intermediate array needed.
Following code moves List items Up or Down, checks for upper;lower bounds, and updates the ListIndex.

Option Explicit
 
Private Sub Form_Load()
   Dim i As Long
   For i = 1 To 10
      ListBox1.AddItem "Item " & Chr(64 + i)
   Next
End Sub
 
Private Sub SpinButton1_SpinDown()
   Dim SelIndex         As Long
   Dim Temp             As String
   
   SelIndex = ListBox1.ListIndex
   If (SelIndex >= 0) And (SelIndex < ListBox1.ListCount - 1) Then
      Temp = ListBox1.List(SelIndex + 1)
      ListBox1.List(SelIndex + 1) = ListBox1.List(SelIndex)
      ListBox1.List(SelIndex) = Temp
      ListBox1.ListIndex = ListBox1.ListIndex + 1
   End If
End Sub
 
Private Sub SpinButton1_SpinUp()
   Dim SelIndex         As Long
   Dim Temp             As String
   
   SelIndex = ListBox1.ListIndex
   If SelIndex > 0 Then
      Temp = ListBox1.List(SelIndex - 1)
      ListBox1.List(SelIndex - 1) = ListBox1.List(SelIndex)
      ListBox1.List(SelIndex) = Temp
      ListBox1.ListIndex = ListBox1.ListIndex - 1
   End If
End Sub

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question