Search Tools Links Login

Listbox Reorder

A function that will allow you to move the selected item in any listbox up or down in the listbox.

Original Author: Troy J


MoveListItem(ListBoxName, (0/1))
(See Code Comments)


-1 if nothing is selected
X is the new position of the selected item


Public Function MoveListItem(LstBox As Object, WhatDir As Integer)
  'WhatDir = 0 up, 1 down
  'Returns -1 if nothing is selected
  'Returns current position otherwise
  Dim CurPos As Integer, CurData As String, NewPos As Integer
  CurPos = LstBox.ListIndex
  If CurPos < 0 Then MoveListItem = -1: Exit Function
  CurData = LstBox.List(CurPos)
  If WhatDir = 0 Then
    'Move Up
    If (CurPos - 1) < 0 Then NewPos = (LstBox.ListCount - 1) Else NewPos = (CurPos - 1)
    'Move Down
    If (CurPos + 1) > (LstBox.ListCount - 1) Then NewPos = 0 Else NewPos = (CurPos + 1)
  End If
  LstBox.RemoveItem (CurPos)
  LstBox.AddItem CurData, NewPos
  LstBox.Selected(NewPos) = True
  MoveListItem = NewPos
End Function

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 75 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.