vb.net - 如何在特定位置/索引处将文本行附加到 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
解决方案
万一其他人面临同样的障碍,我发现通过使用输入/输出库和线程库获得所需的结果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
推荐阅读
- python - 使用 glob 将文件名放入列表
- xml - 如何将xml导入jackrabbit?
- visual-studio-code - 如何更改 VS Code 远程容器中的默认卷挂载?
- c - getline() 与 fgets():控制内存分配
- reactjs - react和react-cookie中的“超出最大更新深度”错误
- jquery - jQuery addClass 方法正在使用过渡
- javascript - 在具有通用 js 文件的不同 cshtml 文件中定义 javascript 变量的正确方法是什么?
- python - 如何在具有 3 个特征的数据集上实现 kmeans 聚类:年龄、体重、一些随机人的身高?
- php - 如何使用 Laravel 通过 maatwebsite 获取导入 xlsx 公式的结果?我得到公式而不是公式的值
- amazon-web-services - DynamoDB 流在表删除/删除后保留