|
|
Title | Display a 10 line scrolling log |
Keywords | log, limit, scroll |
Categories | Tips and Tricks |
|
|
When you add a new line to text in a TextBox, count the carriage returns from beginning to end. When you get to number 10, chop off the rest of the log string.
|
|
Private Sub AddLogLine(ByVal new_text As String)
Const MAX_LINES = 10
Dim all_text As String
Dim num_cr As Integer
Dim pos As Integer
' Add the new text at the end of the current text.
all_text = txtLog.Text & vbCrLf & new_text
' Count the carriage returns from back to front.
pos = InStrRev(all_text, vbCrLf)
Do While pos > 0
num_cr = num_cr + 1
If num_cr >= MAX_LINES Then
' Stop here.
all_text = Mid$(all_text, pos + 2)
Exit Do
End If
pos = InStrRev(all_text, vbCrLf, pos - 2)
Loop
' Display the results and scroll to the bottom.
txtLog.Text = all_text
txtLog.SelStart = Len(txtLog.Text)
End Sub
|
|
Formatted by
Neil Crosby
|
|
|
|
|
|