My little solution in practice
I had a few minutes and I thought I could probably use this myself so I wrote a small class wrapper for the list box. This could be made more elaborate and used in a UserControl if you wish.
Code:
' code for Form
' One ListBox and Two Buttons called cmdUp and cmdDown
Option Explicit
Dim myListBox As New SpecialListBox
Private Sub cmdDown_Click()
myListBox.ScrollDown
End Sub
Private Sub cmdUp_Click()
myListBox.ScrollUp
End Sub
Private Sub Form_Load()
myListBox.LinkedControl = List1
myListBox.VisibleRows = 6
Dim c As Integer
For c = 0 To 10
myListBox.AddItem Str(c)
Next
End Sub
Code:
' this is the class module I called "SpecialListBox"
Option Explicit
Dim mItems() As String
Dim mItemCount As Integer
Dim mLinkedControl As ListBox
Dim mCurrentPos As Integer
Dim mVisibleRows As Integer
Public Property Get List(Index As Integer)
List = mItems(Index)
End Property
Public Property Get VisibleRows() As Integer
VisibleRows = mVisibleRows
End Property
Public Property Let VisibleRows(RowCount As Integer)
mVisibleRows = RowCount
End Property
Public Property Let LinkedControl(TheControl As ListBox)
Set mLinkedControl = TheControl
End Property
Public Property Get LinkedControl() As ListBox
Set LinkedControl = mLinkedControl
End Property
Public Sub AddItem(Item As String, Optional Index As Integer = -1)
mItemCount = mItemCount + 1
ReDim Preserve mItems(mItemCount)
If Index = -1 Then
' add to the end as the default
mItems(mItemCount - 1) = Item
Else
' add to the correct position
Dim c As Integer
' shift all the current items
For c = mItemCount To Index + 1 Step -1
mItems(c) = mItems(c - 1)
Next
mItems(Index) = Item
End If
Refresh
End Sub
Public Sub RemoveItem(Index As Integer)
Dim c As Integer
For c = Index To mItemCount - 1
mItems(c) = mItems(c + 1)
Next
mItemCount = mItemCount - 1
ReDim Preserve mItems(mItemCount)
Refresh
End Sub
Public Sub Refresh()
Dim c As Integer
mLinkedControl.Clear
For c = 0 To mVisibleRows - 1
If c + mCurrentPos >= mItemCount Then Exit For
mLinkedControl.AddItem mItems(c + mCurrentPos)
Next
End Sub
Public Sub Clear()
mItemCount = 0
mCurrentPos = 0
ReDim Preserve mItems(0)
mLinkedControl.Clear
End Sub
Public Property Get ListCount() As Integer
ListCount = mItemCount
End Property
Public Property Let CurrentPosition(NewPos As Integer)
If NewPos < mItemCount And NewPos >= 0 Then
mCurrentPos = NewPos
Refresh
End If
End Property
Public Property Get CurrentPosition() As Integer
CurrentPosition = mCurrentPos
End Property
Public Function ScrollDown() As Boolean
' convenience method to allow scroll down the list
Dim tmp As Integer
tmp = CurrentPosition
CurrentPosition = tmp + 1
ScrollDown = Not (tmp = CurrentPosition)
End Function
Public Function ScrollUp() As Boolean
' convenience method to allow scroll up the list
Dim tmp As Integer
tmp = CurrentPosition
CurrentPosition = tmp - 1
ScrollUp = Not (tmp = CurrentPosition)
End Function
I know you will have found your own solution as well so hopefully my post will help someone else if they ever come across your post while searching in the future.
Regards
Paul Lewis
Well if you want to go and get technical on me :)
Er yes - I will be copying your code and trying it our Aaron Young as it looks like quite a useful thing.
Cheers
Paul Lewis