Scrolling a listbox programmatically

pcc

Well-known Member
Joined
Jan 21, 2003
Messages
1,353
I have created an Excel "jukebox" for playing mp3 files. I also have a database of lyrics for (some of) the songs, with timings in seconds - see sample below. Timings are seconds into the song when the relevant lyric begins. (I don't time them to multiple decimal places, it's done programmatically!)
1​
EaglesNew kid in townThere's talk on the street it sounds so familiar
20.66796875​
2​
EaglesNew kid in townGreat expectations everybody's watching you
29.0859375​
3​
EaglesNew kid in townPeople you meet they all seem to know you
38.83984375​
4​
EaglesNew kid in townEven your old friends treat you like you're something new
47.875​
When the song plays, the lyrics are retrieved from the database, and populate a userform with a listbox. When the time in column 5 is reached, then the corresponding item in the list is highlighted (bit like a karaoke, but that's not what it is..).
If the number of lyrics in the song is greater than that which can be shown in the listbox (say 30), then eventually the bottom item in the listbox is highlighted, and the listbox then scrolls as more lines are achieved and highlighted, the last visible item always being the current and hence highlighted lyric. This means that it's not possible to see upcoming lines until the relevant time is reached, once the current lyric is 30 or more. I would like to always see the next (say) 5 lines.

In essence, I need therefore:
Listbox visible items: 30
If lyric number <= 25 Then
lyrics 1 to 30 are visible so do nothing
Else
scroll listbox by one line to show lyrics 2 to 31 (when current lyric is 26)
scroll listbox by one line to show lyrics 3 to 32 (when current lyric is 27)
etc etc
End If

I can't see how to do this, and wonder if any of you might be able to help. All suggestion welcomed!
Thanks for reading.
Regards

PS second image is what I need..
 

Attachments

  • newkid.jpg
    newkid.jpg
    63.8 KB · Views: 22
  • Slide1.JPG
    Slide1.JPG
    68.4 KB · Views: 22

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
If you first select five lines ahead at the desired scrolling moment and then select the actually desired line, you will get the effect you want.
The code below is meant to be pasted into a Userform module and demonstrates what I mean. It assumes the presence of a ListBox1 with an appropriate height, a CommandButton1 and a CommandButton2.

VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If

Private Type Locals
    IsScrolling As Boolean
    Abort       As Boolean
End Type
Private this As Locals

Private Sub ListBoxScroll(ByVal argLB As MSForms.ListBox, ByVal argLinesAhead As Long)
    Dim Inx     As Long
    Dim NewInx  As Long

    With argLB
        Inx = .ListIndex
        
        If Inx < .ListCount - argLinesAhead Then
            NewInx = Inx + argLinesAhead
        ElseIf Inx < .ListCount - 1 Then
            NewInx = Inx + 1
        Else
            NewInx = Inx
            Inx = Inx - 1
        End If
        
        .Selected(NewInx) = True
        .Selected(Inx + 1) = True
        DoEvents
    End With
End Sub

Private Sub CommandButton1_Click()
    Dim i       As Long
    
    With this
        If .IsScrolling Then Exit Sub
        .IsScrolling = True
        .Abort = False
    End With
    
    With Me.ListBox1
        For i = .ListIndex To .ListCount - 1
            If this.Abort Then Exit For
            ListBoxScroll argLB:=Me.ListBox1, argLinesAhead:=5
            Sleep 100
        Next i
    End With
    
    this.IsScrolling = False
End Sub

Private Sub CommandButton2_Click()
    this.Abort = True
End Sub

Private Sub UserForm_Initialize()
    Dim i   As Long
    With Me.ListBox1
        For i = 1 To 100
            .AddItem i
        Next i
        .Selected(0) = True
    End With
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    this.Abort = True
End Sub
 
Upvote 0
Solution
GWteB, that code does exactly what I need. Thank you very much for your excellent solution!
Regards
pcc
 
Upvote 0
You are welcome and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,214,573
Messages
6,120,318
Members
448,956
Latest member
Adamsxl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top