【问题标题】:How to write cods or find text in vb.net如何在 vb.net 中编写代码或查找文本
【发布时间】:2014-04-08 13:05:37
【问题描述】:

我有两个 TextBox 控件

  • 一个用于搜索的
  • 一个自己的文本

如果我单击“搜索”按钮,我希望文本框中的搜索词突出显示。

【问题讨论】:

标签: vb.net string textbox


【解决方案1】:

假设您正在构建一个桌面应用程序。

如果您只是想突出显示文本内容中与搜索词匹配的第一个词,可以使用String.IndexOf 查找该词。然后您可以使用返回的索引突出显示:

例如,在 WPF 中,您可以向搜索按钮添加点击事件处理程序:

Private Sub SearchButton_Click(sender As Object, e As RoutedEventArgs)
    ' Find index of first match
    Dim index = ContentBox.Text.IndexOf(SearchBox.Text)
    ' Check for match
    If index = -1 Then Return
    ' Set focus
    ContentBox.Focus()
    ' Select first matching word
    ContentBox.SelectionStart = index
    ContentBox.SelectionLength = SearchBox.Text.Length
End Sub

如果要突出显示文本内容中与搜索词匹配的所有词,则需要使用获取搜索起始索引的重载多次调用 String.IndexOf。要突出显示多个单词,您需要使用 WPFWinForms 中可用的 RichTextBox。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多