如何在Richtextbox中添加特定位置的文本行?

问题描述 投票:0回答:1

我有一个lua脚本文件,我想在特定的位置添加一些新的行,所以我们有下面的文本作为例子。

line 1
line 2
line 3

line 8
line 9
line 10

我想在 第三行 前面还加了几行字 第八行到目前为止,我已经尝试过给行做索引,但没有找到一种方法来使用这些索引来编写新的文本行。

 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.net winforms richtextbox
1个回答
0
投票

如果有人面临同样的障碍,我发现通过使用InputOutput库和线程库可以得到所需的结果。Imports System.IO,Imports 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
© www.soinside.com 2019 - 2024. All rights reserved.