Results 1 to 2 of 2

Thread: Move a Item in a list box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2000
    Location
    Brooklyn Ohio
    Posts
    26

    Move item with the mouse

    If I forget to put an item in a list box. Can I put it in and move it where it needs to be.

    Thank you for your help

    I would like to move it with the mouse
    Last edited by Cdogg; Feb 11th, 2001 at 06:05 PM.

  2. #2
    Fanatic Member
    Join Date
    Jan 2001
    Location
    Vietnam
    Posts
    613
    Add 4 command buttons, a list box with names
    cmdAdd, cmdDelete, cmdUp, cmdDown, lstItems

    Now copy the code below. This code allows you to add,
    delete, move up, move down, any item in the list box

    Code:
    Option Explicit
    
    Private Sub cmdAdd_Click()
      Dim sTmp As String
      sTmp = InputBox("Enter new item to add:")
      If Len(sTmp) = 0 Then Exit Sub
      lstItems.AddItem sTmp
    End Sub
    
    Private Sub cmdDelete_Click()
      If lstItems.ListIndex > -1 Then
        If MsgBox("Delete '" & lstItems.Text & "'?", vbQuestion + vbYesNo) = vbYes Then
          lstItems.RemoveItem lstItems.ListIndex
        End If
      End If
    End Sub
    
    Private Sub cmdUp_Click()
      On Error Resume Next
      Dim nItem As Integer
      
      With lstItems
        If .ListIndex < 0 Then Exit Sub
        nItem = .ListIndex
        If nItem = 0 Then Exit Sub  'can't move 1st item up
        'move item up
        .AddItem .Text, nItem - 1
        'remove old item
        .RemoveItem nItem + 1
        'select the item that was just moved
        .Selected(nItem - 1) = True
      End With
    End Sub
    
    Private Sub cmdDown_Click()
      On Error Resume Next
      Dim nItem As Integer
      
      With lstItems
        If .ListIndex < 0 Then Exit Sub
        nItem = .ListIndex
        If nItem = .ListCount - 1 Then Exit Sub 'can't move last item down
        'move item down
        .AddItem .Text, nItem + 2
        'remove old item
        .RemoveItem nItem
        'select the item that was just moved
        .Selected(nItem + 1) = True
      End With
    End Sub
    
    
    Private Sub lstItems_Click()
      SetListButtons
    End Sub
    
    Sub SetListButtons()
      Dim i As Integer
      i = lstItems.ListIndex
      'set the state of the move buttons
      cmdUp.Enabled = (i > 0)
      cmdDown.Enabled = ((i > -1) And (i < (lstItems.ListCount - 1)))
      cmdDelete.Enabled = (i > -1)
    End Sub
    Hope this helps
    TheBao

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