Imports System.Windows.Forms Namespace DebugLog ''' ''' 考虑修改为自定义控件 ''' Public Class ControlRecord Sub New(recordControl As RichTextBox) ShowRecord = True SuspendLayout = False MaxRecordCount = 512 RtxRecord = recordControl End Sub ''' ''' 是否在添加内容时先挂起布局 ''' ''' Property SuspendLayout() As Boolean ''' ''' 是否添加记录到控件 ''' ''' Property ShowRecord() As Boolean ''' ''' 控件记录最大行数 ''' ''' Property MaxRecordCount() As Integer ''' ''' 需要被添加数据记录的控件句柄 ''' ''' Property RtxRecord() As RichTextBox ''' ''' 清空内容 ''' Public Sub Clear() If RtxRecord Is Nothing Then Return RtxRecord.Clear() End Sub Public Sub AppendRecord(logString As String) If ShowRecord = False Then Return If RtxRecord Is Nothing Then Return If RtxRecord.InvokeRequired Then '判断是否需要开委托 RtxRecord.Invoke(New Action(Of String)(AddressOf AppendRecord), New Object() {logString}) Return End If With RtxRecord If SuspendLayout Then .SuspendLayout() If .Lines.Length > MaxRecordCount Then '超过上限则移除最初行内容 .ReadOnly = False .SelectionStart = 0 .SelectionLength = .GetFirstCharIndexFromLine(1) .SelectedText = String.Empty .ReadOnly = True .SelectionStart = .TextLength End If .AppendText($"{Now:yyyy:MM:dd HH:mm:ss} - {logString}{vbNewLine}") .ScrollToCaret() .ResumeLayout(False) Else If .Lines.Length > MaxRecordCount Then '超过上限则移除最初行内容 .ReadOnly = False .SelectionStart = 0 .SelectionLength = .GetFirstCharIndexFromLine(1) .SelectedText = String.Empty .ReadOnly = True .SelectionStart = .TextLength End If .AppendText($"{Now:yyyy:MM:dd HH:mm:ss} - {logString}{vbNewLine}") .ScrollToCaret() End If End With End Sub End Class End Namespace