[vb.net]如何在richtextbox中查找关键字的位置(所在行),然后删除所在行上面或者下面的行
展开全部
假设richtextbox1中有这样一些文本:
-------------------------
[vb.net]如何在
richtextbox
中查找关键字的位置
(所在行),然后删除
所在行上面或者
下面的行
--------------------------
找到关键字 "然后" 删除相邻行 代码如下:
Dim Lines() As String = RichTextBox1.Lines
Dim index As Integer = RichTextBox1.Find("然后", 0, RichTextBoxFinds.MatchCase)
Dim TargetLine As Integer = RichTextBox1.GetLineFromCharIndex(index)
Lines(TargetLine - 1) = ""
Lines(TargetLine + 1) = ""
RichTextBox1.Clear()
For i = 0 To Lines.Length - 1
RichTextBox1.Text = RichTextBox1.Text & Lines(i) & vbCrLf
Next
-------------------------
[vb.net]如何在
richtextbox
中查找关键字的位置
(所在行),然后删除
所在行上面或者
下面的行
--------------------------
找到关键字 "然后" 删除相邻行 代码如下:
Dim Lines() As String = RichTextBox1.Lines
Dim index As Integer = RichTextBox1.Find("然后", 0, RichTextBoxFinds.MatchCase)
Dim TargetLine As Integer = RichTextBox1.GetLineFromCharIndex(index)
Lines(TargetLine - 1) = ""
Lines(TargetLine + 1) = ""
RichTextBox1.Clear()
For i = 0 To Lines.Length - 1
RichTextBox1.Text = RichTextBox1.Text & Lines(i) & vbCrLf
Next
更多追问追答
追问
如果要是删除上面 或者 下面所有的行怎么删除?
追答
Enum UpDown
Up = 0
Down = 1
End Enum
Private Sub DeleteText(ByVal _rich As RichTextBox, ByVal Key As String, ByVal Target As UpDown)
Dim Lines() As String = RichTextBox1.Lines
Dim index As Integer = RichTextBox1.Find("然后", 0, RichTextBoxFinds.MatchCase)
Dim TargetLine As Integer = RichTextBox1.GetLineFromCharIndex(index)
RichTextBox1.Clear()
Select Case Target
Case UpDown.Up
For i = 0 To TargetLine - 1
Lines(i) = ""
Next
Case UpDown.Down
For i = Lines.Length - 1 To TargetLine + 1 Step -1
Lines(i) = ""
Next
End Select
For i = 0 To Lines.Length - 1
RichTextBox1.Text = RichTextBox1.Text & Lines(i) & vbCrLf
Next
End Sub
调用: DeleteText(RichTextBox1, "然后", UpDown.Down) '删除以下
DeleteText(RichTextBox1, "然后", UpDown.Up)'删除以上
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询