VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Listbox Reorder

by Troy J (1 Submission)
Category: Miscellaneous
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

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

Inputs
MoveListItem(ListBoxName, (0/1)) (See Code Comments)
Code Returns
-1 if nothing is selected X is the new position of the selected item

Rate Listbox Reorder

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

Download this snippet    Add to My Saved Code

Listbox Reorder Comments

No comments have been posted about Listbox Reorder. Why not be the first to post a comment about Listbox Reorder.

Post your comment

Subject:
Message:
0/1000 characters