Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Location
    NY, USA
    Posts
    132
    Thanks
    1
    Thanked 1 Time in 1 Post

    Visual Basic list box

    Hi,

    I'm using visual basic 6. I have a list box, and I add some items to it like this:
    Code:
    list.AddItem "Item 1", 0
    list.AddItem "Item 2", 1
    list.AddItem "Item 3", 2
    Then I have a button, and I want it so when the user clicks the button, I want the item thats selected to move up in position 1 slot. I tried something like this:
    Code:
    list.ListIndex = list.ListIndex - 1
    But that just changes the selection bar thing, and not the position of the item.

    How would I go about doing this?

    Thanks for your help,
    ~David

  • #2
    New Coder
    Join Date
    Feb 2005
    Location
    Texas
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Give this a try.

    Code:
    Private Sub cmdDown_Click()
      Dim Temp As String
      With List1
        If .ListIndex < .ListCount - 1 And .ListIndex >= 0 Then
          Temp = .List(.ListIndex)
          .List(.ListIndex) = .List(.ListIndex + 1)
          .List(.ListIndex + 1) = Temp
          .ListIndex = .ListIndex + 1
        End If
      End With
    End Sub
    
    Private Sub cmdUp_Click()
      Dim Temp As String
      With List1
        If .ListIndex > 0 Then
          Temp = .List(.ListIndex)
          .List(.ListIndex) = .List(.ListIndex - 1)
          .List(.ListIndex - 1) = Temp
          .ListIndex = .ListIndex - 1
        End If
      End With
    End Sub
    Binary Invisions
    Where the Webmasters Go.
    www.binvisions.com

  • #3
    Regular Coder
    Join Date
    Jan 2005
    Location
    NY, USA
    Posts
    132
    Thanks
    1
    Thanked 1 Time in 1 Post
    Hi,

    It works perfectly. You just have to change the With List1 to with list.

    Thanks sooo much for your help,
    ~David


  •  

    Posting Permissions

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