Search Tools Links Login

Listbox Reorder


Visual Basic 6, or VB Classic

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

Original Author: Troy J

Inputs

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

Returns

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

Code

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)
  Else
    '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: 96 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.