VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Shows how to move an item in a list box.

by mohamud ibrahim (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 26th March 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Shows how to move an item in a list box.

Rate Shows how to move an item in a list box.



  On Error Resume Next
  Dim nItem As Integer
  
  With lstSelected
    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 lstSelected
    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 cmdRightOne_Click()
  On Error Resume Next
  Dim i As Integer
  
  If lstAll.ListCount = 0 Then Exit Sub
  
  lstSelected.AddItem lstAll.Text
  i = lstAll.ListIndex
  lstAll.RemoveItem lstAll.ListIndex
  If lstAll.ListCount > 0 Then
    If i > lstAll.ListCount - 1 Then
      lstAll.ListIndex = i - 1
    Else
      lstAll.ListIndex = i
    End If
  End If
  lstSelected.ListIndex = lstSelected.NewIndex
End Sub
Private Sub cmdRightAll_Click()
  On Error Resume Next
  Dim i As Integer
  For i = 0 To lstAll.ListCount - 1
    lstSelected.AddItem lstAll.List(i)
  Next
  lstAll.Clear
  lstSelected.ListIndex = 0
End Sub
Private Sub cmdLeftOne_Click()
  On Error Resume Next
  Dim i As Integer
  
  If lstSelected.ListCount = 0 Then Exit Sub
  
  lstAll.AddItem lstSelected.Text
  i = lstSelected.ListIndex
  lstSelected.RemoveItem i
  
  lstAll.ListIndex = lstAll.NewIndex
  If lstSelected.ListCount > 0 Then
    If i > lstSelected.ListCount - 1 Then
      lstSelected.ListIndex = i - 1
    Else
      lstSelected.ListIndex = i
    End If
  End If
End Sub
Private Sub cmdLeftAll_Click()
  On Error Resume Next
  Dim i As Integer
  For i = 0 To lstSelected.ListCount - 1
    lstAll.AddItem lstSelected.List(i)
  Next
  lstSelected.Clear
  lstAll.ListIndex = lstAll.NewIndex

End Sub
Private Sub Form_Load()
  lstAll.AddItem "aaa"
  lstAll.AddItem "bbb"
  lstAll.AddItem "ccc"
  lstAll.ListIndex = 0
End Sub
Private Sub lstAll_DblClick()
  cmdRightOne_Click
End Sub
Private Sub lstSelected_DblClick()
  cmdLeftOne_Click
End Sub


Download this snippet    Add to My Saved Code

Shows how to move an item in a list box. Comments

No comments have been posted about Shows how to move an item in a list box.. Why not be the first to post a comment about Shows how to move an item in a list box..

Post your comment

Subject:
Message:
0/1000 characters