Results 1 to 5 of 5

Thread: Listbox

  1. #1

    Thread Starter
    Addicted Member Rudy's Avatar
    Join Date
    Sep 2000
    Location
    BC, Canada
    Posts
    198
    I want to be able to move my items in the list box, you know, like i'll add a "down" button and an "up" one, then the selected item will go up or down....but how can i do that??
    *Rudy^
    Visual Studio 6 Ent. SP5
    Windows 2000 SP4
    Windows XP SP1a

  2. #2
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722
    Here is a really ****** way of doing it, but it works:
    Code:
    'Up button
    Private Sub Command1_Click()
        If List1.ListIndex <> -1 Then
            MoveLine "UP"
        End If
    End Sub
    
    'Down button
    Private Sub Command2_Click()
        If List1.ListIndex <> -1 Then
            MoveLine "DOWN"
        End If
    End Sub
    
    'Custom Sub
    Private Sub MoveLine(ByVal direction as String)
        Dim i As Integer 'For counting
        Dim hilite As Integer 'Hilighted List Item Index
        Dim strTemp As String 'Temp string
    
        i = List1.ListCount
        
        ReDim ListItems(i) 'Array for list items
        
        For i = 0 To List1.ListCount
            ListItems(i) = List1.List(i) 'copy list items to array
        Next
        
        hilite = List1.ListIndex 'save hilighted index
        
        Select Case UCase(direction)
            Case "UP"
                If hilite = 0 Then 'We are at the top of the list. Do nothing.
                    Exit Sub
                End If
    
                i = List1.ListIndex - 1 'swop selected item with item above
                strTemp = ListItems(i)  '
                ListItems(i) = ListItems(List1.ListIndex)'
                ListItems(List1.ListIndex) = strTemp'
                List1.Clear 'clear list
                For i = 0 To UBound(ListItems) - 1
                    List1.AddItem ListItems(i) 're-populate with new list
                Next
                List1.ListIndex = hilite - 1 'move hilight
            Case "DOWN"
                If hilite = List1.ListCount - 1 Then 'We are at the bottom of the list. Do nothing
                    Exit Sub
                End If
                i = List1.ListIndex + 1 'swop selected item with item below
                strTemp = ListItems(i) '
                ListItems(i) = ListItems(List1.ListIndex)'
                ListItems(List1.ListIndex) = strTemp'
                List1.Clear 'clear list
                For i = 0 To UBound(ListItems) - 1
                    List1.AddItem ListItems(i) 're-populate with new list
                Next
                List1.ListIndex = hilite + 1 'move hilight
        End Select
    End Sub

    r0ach™
    Don't forget to rate the post

  3. #3
    Lively Member Surgeon's Avatar
    Join Date
    Oct 2000
    Posts
    121

    Wink

    here's another solution, maybe easier :-)

    Private Sub CommandUP_Click()
    Dim temporary As String
    Dim k As Long
    k = List1.ListIndex ' k=the position of the current selected line
    If k = -1 Then Exit Sub 'the listbox hasn't any line selected
    If k = 0 Then Exit Sub ' the selected line is already the first, so there's no where UP to go
    temporary = List1.List(k - 1)
    List1.List(k - 1) = List1.List(k)
    List1.List(k) = temporary
    List1.Selected(k - 1) = True
    End Sub

    Private Sub CommandDOWN_Click()
    Dim temporary As String
    Dim k As Long
    k = List1.ListIndex ' k=the position of the current selected line
    If k = -1 Then Exit Sub 'the listbox hasn't any line selected
    If k = List1.ListCount - 1 Then Exit Sub ' the selected line is already the last, so there's no where DOWN to go
    temporary = List1.List(k + 1)
    List1.List(k + 1) = List1.List(k)
    List1.List(k) = temporary
    List1.Selected(k + 1) = True
    End Sub

  4. #4
    Addicted Member Shrog's Avatar
    Join Date
    Aug 1999
    Location
    Darkest Africa
    Posts
    186

    Smile What about the ItemData?

    Don'r forget about item data. you are moving around the item (text) but not the ItemData (numeric value that accompanies the item).

    Code:
    Public Enum nfslDirection
      nfslUp = 1
      nfslDown = -1
    End Enum
    
    Private Sub MoveItem(ByVal vDirection As nfslDirection)
      Dim TempItem As String
      Dim TempItemData As Long
      Dim SourceIndex As Long
      Dim TargetIndex As Long
      
      SourceIndex = lstBox.ListIndex
      TargetIndex = SourceIndex - vDirection
      If (TargetIndex < 0) Or (TargetIndex >= lstBox.ListCount) Or SourceIndex < 0 Then
        Beep
        Exit Sub
      End If
      
      TempItem = lstBox.List(SourceIndex)
      TempItemData = lstBox.ItemData(SourceIndex)
      Call lstBox.RemoveItem(SourceIndex)
      Call lstBox.AddItem(TempItem, TargetIndex)
      ListB.ItemData(lstBox.NewIndex) = TempItemData
      ListB.Selected(lstBox.NewIndex) = True
        
    End Sub
    Shrog

  5. #5

    Thread Starter
    Addicted Member Rudy's Avatar
    Join Date
    Sep 2000
    Location
    BC, Canada
    Posts
    198
    it's ok, because all i really need is the text, i don't really need the numeric value but thanx anywayz i might need it in another project
    *Rudy^
    Visual Studio 6 Ent. SP5
    Windows 2000 SP4
    Windows XP SP1a

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width