0

我想在 vb.net 的文本框中选择一个单词到另一个单词,并突出显示它们之间的所有内容。

一个例子是

我去了海滩,和家人一起野餐,然后在 6 点钟回家。

起始词 behad和结束词 beinghome以及其间突出显示的所有内容。

我已经使用了一些代码

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim a As String
    Dim b As String
    a = TextBox2.Text 'means what ever is in textbox2 string to the location where "a" is
    b = InStr(RichTextBox1.Text, a)
    If b Then
        RichTextBox1.Focus()
        RichTextBox1.SelectionStart = b - 1
        RichTextBox1.SelectionLength = Len(a)

但它不完全是我想要它做的。

除此之外,还使用了 RegEx 函数,如下所示

  'gets rid of the enter line break eg <enter> command no new lines
     Dim content As String = Replace(TextBox1.Text, Global.Microsoft.VisualBasic.ChrW(10), Nothing)
    'searches for this tag in the brackets between ".*" will be the contents
    Dim Regex As New Regex("<div.*class=""answer_text"".*id=editorText"".*""")
    'Show the string 
    For Each M As Match In Regex.Matches(content)
    'This will get the values, there are 3 atm meta.name des and content
    Dim Description As String = M.Value.Split("""").GetValue(3)
    'displays the content in the label
    TextBox3.Text = "" & Description
    Next
4

2 回答 2

1

这将选择 startWord 和 endWord 之间的所有内容,不包括它们

Dim startWord As String = "had"
Dim endWord As String = "home"

Dim index As Integer = richTextBox1.Text.IndexOf(startWord)
richTextBox1.[Select](index + startWord.Length, richTextBox1.Text.IndexOf(endWord) - index - startWord.Length)
于 2013-03-06T14:16:19.213 回答
0

这是一个涉及两个TextBox控件的解决方案:

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Dim a As String
        Dim b As String
        Dim index_a As Integer
        Dim index_b As Integer
        a = TextBox1.Text
        b = TextBox2.Text
        index_a = InStr(RichTextBox1.Text, a)
        index_b = InStr(RichTextBox1.Text, b)
        If index_a And index_b Then
            RichTextBox1.Focus()
            RichTextBox1.SelectionStart = index_a - 1
            RichTextBox1.SelectionLength = (index_b - index_a) + Len(b)
        End If
End Sub

TextBox1包含第一个单词,TextBox2包含第二个单词。单击按钮时,它将从第一个单词突出显示到第二个单词的末尾。

于 2013-03-06T14:47:13.300 回答