I was doing this in my code.
vbnet
Private _Lines As New Collections.Specialized.StringCollection
Private Const _MaxLines As Int16 = 30
Private Sub SetMessages(ByVal message As String)
If _Lines.Count > _MaxLines Then
_Lines.RemoveAt(_MaxLines - 1)
End If
_Lines.Add(DateTime.Now.ToString("HH:mm:ss") & " - " & message & Environment.NewLine)
Dim text As New System.Text.StringBuilder
For _temp As Integer = _Lines.Count - 1 To 0 Step -1
text.Append(_Lines(_temp))
Next
Me.txtMessages.Text = text.ToString
End Sub
Look Mama, I made a queue.
Oh wait, there is a queue object that I should have used.
vbnet
Private _Lines As New Queue(Of String)(30)
Private Const _MaxLines As Int16 = 30
Private Sub SetMessages(ByVal message As String)
_Lines.Enqueue(DateTime.Now.ToString("HH:mm:ss") & " - " & message & Environment.NewLine)
If _lines.Count > _maxlines Then _lines.Dequeue()
Me.txtMessages.Text = ""
For Each line As String In _lines.Reverse
Me.txtMessages.AppendText(line & Environment.NewLine)
Next
End Sub
That is much shorter. Sometimes I amaze myself ;-).
Of course, it would be even shorter if I had used a ListBox.
vbnet
Private _Lines As New Queue(Of String)(30)
Private Const _MaxLines As Int16 = 30
Private Sub SetMessages(ByVal message As String)
_Lines.Enqueue(DateTime.Now.ToString("HH:mm:ss") & " - " & message & Environment.NewLine)
If _lines.Count > _maxlines Then _lines.Dequeue()
Me.ListBox1.DataSource = _lines.Reverse.ToList()
End Sub
So use the power of Q.
Just for Denis I did a followup on this post.