如何为“查找”功能的文本添加突出显示?

时间:2014-09-06 19:22:55

标签: vb.net highlight

我正在尝试使用一个包含大量文本的文本框和另一个在第一个文本框中找到字符串的文本框。我用它来查找字符串是否位于:

    Text1 = TextBox1.Text
    WordtoFind = TextBox2.Text
    O = Text1.IndexOf(WordtoFind)

    If (O = -1) Then
        Label.Text = "String was not found"
    Else
        Label.Text = "String was found"
    End If

有人可以通过突出显示找到的单词来帮助我吗?感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

If (O = -1) Then
    Label.Text = "String was not found"
Else
    Label.Text = "String was found"
    TextBox1.Focus()
    TextBox1.Select(O, WordtoFind.Length)
End If

修改

Dim wordPosition As Integer

Text1 = TextBox1.Text
WordtoFind = TextBox2.Text
wordPosition = Text1.IndexOf(WordtoFind)

If (wordPosition = -1) Then
    Label.Text = "String was not found"
Else
    Label.Text = "String was found"
    TextBox1.Focus()
    TextBox1.Select(wordPosition, WordtoFind.Length)
End If

瓦尔特