【问题标题】:Search richtextbox for string match and then record the index in vb.net在richtextbox中搜索字符串匹配,然后在vb.net中记录索引
【发布时间】:2016-01-08 21:09:46
【问题描述】:

我有一个富文本框,每行都填充了数据。我想读取每一行并在列表中找到匹配的单词并记录它的索引值,然后退出循环。

然后在程序中的某个时刻,我想读取相同的richtextbox并复制我之前获得的匹配索引的行数据。

到目前为止,我已经设法做到以下几点:

For Each item As String In rtb_critical.Lines

 If item.Contains("Failed to initialise") = True Then
            MsgBox("yes")
Else
 MsgBox("no")
End if
Next

我也找到了这段代码,但不知道如何使用它来获得我想要的。

Dim index As Integer = 0

 Do
    ' Find occurrences of the search word, incrementing  
    ' the start index. 
    index = RichTextBox1.Find(searchWord, index + 1, _
        RichTextBoxFinds.MatchCase)
    If (index <> -1) Then

      lineList.Add(RichTextBox1.GetLineFromCharIndex(index))
    End If
Loop While (index <> -1)

谁能帮帮我?

谢谢

【问题讨论】:

  • 你真的需要 RichTextBox 吗?如果只是纯文本,我建议你使用打开多行的 TextBox - 你会避免很多问题。
  • 你提到我会避免问题,我的情况是什么?
  • 只要您非常小心,只有文本进入 RTB,您可能会没事的。如果用户可以粘贴,如果复制文本的程序知道 RTF,您可能会在文本中得到 RTF 代码。

标签: vb.net search richtextbox


【解决方案1】:

您可以使用从 0 到 RTB 的 Lines.Count -1 的 for 循环。该迭代器变量将成为索引位置。

因此,当找到一行时,您的迭代器变量就是要存储的索引

for i as integer = 0 to rtb1.lines.count-1
   if rtb1.lines(i).Contains("a search string")
     'store i as that holds the index of the line found
   end if
next

【讨论】:

  • 这可以获取索引,但是如果我想稍后读取具有此索引值的行并将其复制到另一个richtextbox,我该怎么做?我尝试了 rtb2.Lines = rtb1.Lines(index),但它给出了错误。
  • 想通了,应该是rtb2.text
猜你喜欢
  • 1970-01-01
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多