|
|
Title | Display a 10 line scrolling log using Split and Join |
Keywords | log, limit, scroll |
Categories | Tips and Tricks |
|
|
When you add a new line, use Split to break the lines into an array. If there are more than 10 lines, move 10 lines to the front of the array, resize the array to remove the excess lines, and use Join to concatenate the lines back into a string.
|
|
Private Sub AddLogLine(ByVal new_text As String)
Const MAX_LINES = 10
Dim txt As String
Dim lines As Variant
Dim offset As Integer
Dim i As Integer
' Add the new text at the end of the current text.
txt = txtLog.Text & vbCrLf & new_text
' Break the log into lines.
lines = Split(txt, vbCrLf)
' If there are more than 10 lines, remove some.
If UBound(lines) >= MAX_LINES Then
' Move the lines down.
offset = UBound(lines) - MAX_LINES + 1
For i = offset To UBound(lines)
lines(i - offset) = lines(i)
Next i
ReDim Preserve lines(0 To MAX_LINES - 1)
txt = Join(lines, vbCrLf)
End If
txtLog.Text = txt
txtLog.SelStart = Len(txtLog.Text)
End Sub
|
|
|
|
|
|