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)

しかし、それはまさに私がやりたいことではありません。

これに加えて、以下に示すように正規表現関数を使用していました

  '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

TextBox2 つのコントロールを含むソリューションを次に示します。

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含み、2 番目の単語を含みます。ボタンをクリックすると、最初の単語から 2 番目の単語の終わりまでが強調表示されます。

于 2013-03-06T14:47:13.300 に答える