首页 > 解决方案 > 如何在特定位置/索引处将文本行附加到 Richtextbox 中?

问题描述

我有一个 lua 脚本文件,我想在其中的特定位置添加一些新行。因此,假设我们有以下文本作为示例:

line 1
line 2
line 3

line 8
line 9
line 10

我想在第 3行之后插入一些新行,并在第 8行之前插入一些额外的行 到目前为止,我已经尝试对这些行进行索引,但没有找到一种方法来使用这些索引来编写新的文本行。

 For i As Integer = 0 To textbox.Lines.Count - 1
    Dim x As Integer = i + 1
    Dim y As Integer = i - 1
    If textbox.Lines(i).Contains("line 3") Then
        textbox.Lines(x).Append("Line 4")
    End If
 Next

标签: vb.netwinformsrichtextbox

解决方案


万一其他人面临同样的障碍,我发现通过使用输入/输出库和线程库获得所需的结果Imports System.IOImports System.Threading结合以下for循环:

For i As Integer = 0 To textbox.Lines.Length - 1
        Dim s As String = textbox.Lines(i)
        Dim index As Integer = s.IndexOf("line 3")
        If index > -1 Then
            Dim length As Integer = s.Length - index
            index += textbox.GetFirstCharIndexFromLine(i)
            textbox.Select(index + length, 1)
            Thread.Sleep(1)
            SendKeys.SendWait("{ENTER}")
            textbox.Text = textbox.Text.Insert(textbox.GetFirstCharIndexOfCurrentLine,
                                       "line 4" & vbCrLf &
                                       "line 5 " & vbCrLf &
                                       "line 6" & vbCrLf &
                                       "line 7" & vbCrLf)
        End If
    Next

推荐阅读