ListBox sorting

Discussion in 'AutoCAD' started by Pat Bjork, Feb 6, 2004.

  1. Pat Bjork

    Pat Bjork Guest

    I would like to be able to move a selected item up or down in a list box,
    and I can't figure out how. An example would be to have a listbox with 5
    items in it. I would like to select the third item, then select the "Move
    Up" command button, and then have item 3 swith with item 2. I appreciate any
    help you can give.

    Before After
    1 1
    2 3
    3 2
    4 4
    5 5

    Thanks,

    Pat
     
    Pat Bjork, Feb 6, 2004
    #1
  2. Private Sub cmdUp_Click()
    Dim curIdx As Long
    Dim varTemp As Variant
    curIdx = lst.ListIndex
    If curIdx > 0 Then
    varTemp = lst.List(curIdx - 1)
    lst.List(curIdx - 1) = lst.List(curIdx)
    lst.List(curIdx) = varTemp
    lst.ListIndex = curIdx - 1
    End If
    End Sub

    Private Sub cmdDown_Click()
    Dim curIdx As Long
    Dim varTemp As Variant
    curIdx = lst.ListIndex
    If curIdx < lst.ListCount - 1 And curIdx > -1 Then
    varTemp = lst.List(curIdx + 1)
    lst.List(curIdx + 1) = lst.List(curIdx)
    lst.List(curIdx) = varTemp
    lst.ListIndex = curIdx + 1
    End If
    End Sub

    --James
     
    James Belshan, Feb 6, 2004
    #2
  3. Pat Bjork

    Pat Bjork Guest

    Thanks James,

    That works great. I tried for a couple of hours this morning but could'nt
    figure it out. I was able to put my variable in your routine and it worked.

    Thanks again,

    Pat
     
    Pat Bjork, Feb 6, 2004
    #3
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.