gpt4 book ai didi

vb.net - 如何以编程方式从 ReadOnly RichTextBox 中删除文本?

转载 作者:行者123 更新时间:2023-12-03 00:46:20 25 4
gpt4 key购买 nike

以下代码旨在在ReadOnly RichTextBox 中维护一个文本缓冲区,存储最大数量的字符,并始终保持滚动到底部。它传输实时日志。

但在我尝试保持最大字符数的过程中,rtMessages.TextLength()rtMessages.SelectedText = String.Empty 之后没有发生变化,因此,没有防御性 If block ,我最终会陷入一个无限循环,试图重复删除缓冲区的第一行。

当我删除 RichTextBoxReadOnly 属性时,此功能成功。似乎有点奇怪,因为 AppendText 成功了,但我知道选择是一个不同的野兽。

我可以让 ReadOnly RichTextBox 可以以编程方式修改吗?

Private Sub onNewData(ByRef data As String) Handles _server.clientSentData

' Add new text
rtMessages.SelectionStart = rtMessages.TextLength()
rtMessages.AppendText(data)

' Delete oldest text line-by-line until the whole buffer is shorter than MAX_TEXT_LENGTH characters
Const MAX_TEXT_LENGTH = 200
Dim textLength = rtMessages.TextLength()
While textLength > MAX_TEXT_LENGTH
Dim i As Int16 = 0
Do While rtMessages.GetLineFromCharIndex(i) < 1
i += 1
Loop

rtMessages.Select()
rtMessages.SelectionStart = 0
rtMessages.SelectionLength = i
rtMessages.SelectedText = String.Empty
rtMessages.SelectionLength = 0

If rtMessages.TextLength() = textLength Then
rtMessages.Clear()
rtMessages.AppendText("[buffer trimming algorithm failed]")
Exit While
End If

textLength = rtMessages.TextLength()
End While

' Scroll down
rtMessages.SelectionStart = rtMessages.TextLength()
rtMessages.ScrollToCaret()
End Sub

最佳答案

虽然尝试替换 ReadOnly RichTextBox 中的 SelectedText 不起作用,但使用 SelectedRtf 确实有效:

  'rtMessages.Select()
'rtMessages.SelectionStart = 0
'rtMessages.SelectionLength = i
'rtMessages.SelectedText = String.Empty
'rtMessages.SelectionLength = 0

rtMessages.Select(0, i)
rtMessages.SelectedRtf = "{\rtf1\ansi}"

关于vb.net - 如何以编程方式从 ReadOnly RichTextBox 中删除文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13318238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com