RichTextBox中的“查找下一个”功能:包含“匹配大小写”

时间:2011-05-20 06:33:47

标签: .net vb.net find replace

我知道这个问题已经被问了很多次,但我有一个特定的要求。这可能有点难以理解,为此我道歉。我会尝试尽力评论代码。如果您需要澄清,请告诉我。

我的问题是我有非常复杂,不必要的代码,因为我自己写了这个,这对我来说是一个新概念。该代码旨在在主窗口的RichTextBox中找到一个字符串(该字符串显然是由用户提供的)。代码工作正常,但由于其复杂的性质,我不知道如何实现“匹配案例”功能。

表单包含这些控件(至少是搜索文本时使用的控件):

  • 文本框:txtFind - 用户在此处输入搜索字词
  • 复选框:chkMatchCase - 允许用户选择是否匹配搜索字词的大小写
  • 按钮:btnFind - “查找下一个”按钮

这是我的代码:

Dim index As Integer = 0

Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click
    Dim strSearchTerm As String
    Dim lastIndex As Integer
    Dim strLastSearch As Integer

    'Set the end of the find location to the last known instance of the search term
    lastIndex = frmMain.rtxtNotepad.Text.LastIndexOf(Me.txtFind.Text)
    'If the last known location is 0 (meaning the string is located at the beginning of the document), set strSearchTerm to 0.
    If lastIndex = 0 Then
        strSearchTerm = 0
    ElseIf lastIndex = -1 Then
        'If the search term appears not to exist, double-check (due to an error when searching using Text.LastIndexOf and the case of the search term does not match the instance):
        If frmMain.rtxtNotepad.Text = "" Then
            '   1a) If the main window's RTB is empty, warn the user
            MsgBox("Cannot find '" & txtFind.Text & "'.")
        Else
            '   1b) If the RTB is not empty, search again. 
            If frmMain.rtxtNotepad.Find(Me.txtFind.Text, 0, frmMain.rtxtNotepad.Text.Length, RichTextBoxFinds.None) = -1 Then
                '   2a) If the search string is not found again, warn the user.
                MsgBox("Cannot find '" & txtFind.Text & "'.")
            Else
                '   2b) If it is found, set strSearchTerm to the beginning index of the occurrence.
                strSearchTerm = frmMain.rtxtNotepad.Find(Me.txtFind.Text, 0, frmMain.rtxtNotepad.Text.Length, RichTextBoxFinds.None)
            End If
        End If
    Else
        'If none of the above apply, set strSearchTerm to the index of the occurence
        strSearchTerm = frmMain.rtxtNotepad.Find(Me.txtFind.Text, index, lastIndex, RichTextBoxFinds.None)
    End If

    If strSearchTerm = -1 Then
        'If the search term is found, but this is the last instance, loop back
        strLastSearch = frmMain.rtxtNotepad.Text.LastIndexOf(Me.txtFind.Text)

        frmMain.Focus()
        frmMain.rtxtNotepad.SelectionStart = strLastSearch
        frmMain.rtxtNotepad.SelectionLength = Me.txtFind.Text.Length

        index = 0
    Else
        'If the search term is found, and this is not the last instance, set the starting integer of the Find statement to bypass the previous occurrence of the search term
        frmMain.Focus()
        frmMain.rtxtNotepad.SelectionStart = strSearchTerm
        frmMain.rtxtNotepad.SelectionLength = Me.txtFind.Text.Length

        index = strSearchTerm + 1
    End If
End Sub

无论如何,如果有更好的方法来实现这一点,请告诉我。我做了大量的搜索,但没有找到好的解决方案。我最终将其他一些教程的一小部分组合到了这里。如果没有更好的解决方案,我只想知道如何实现“匹配案例”功能。

对不起,感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用带有Find选项的RichTextBoxFinds方法的任何重载 - 它允许按案例开启/关闭匹配。

您的代码可以简单得多。例如,假设您具有存储最后找到的位置的类/表单级别变量LastFoundLocation。零值表示从开始搜索,而-1表示从当前光标位置搜索。因此按钮处理程序中的相关C#代码将是

if (LastFoundLocation < 0) 
{
   // set it to current cursor location
   LastFoundLocation = frmMain.rtxtNotepad.SelectionStart;
}
LastFoundLocation = frmMain.rtxtNotepad.Find(txtFind.Text, LastFoundLocation, chkMatchCase.Checked ? RichTextBoxFinds.MatchCase : RichTextBoxFinds.None);
if (LastFoundLocation < 0) 
{
   // Not Found
}
else
{
   // Found and word will be highlighted
}