"Find Next" RichTextBox 中的功能:包含"Match Case"



我知道这个问题已经被问了很多次了,但我有一个具体的要求。这可能有点难以理解,对此我深表歉意。我会尽力注释代码。如果您需要澄清,请告诉我。

我的问题是我有非常复杂,不必要的代码,因为我自己写的,这对我来说是一个新概念。该代码旨在在主窗口的 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

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

很抱歉给您带来麻烦,感谢您的帮助!

使用任何采用 RichTextBoxFind 选项的 Find 方法重载 - 它允许按大小写开/关进行匹配。

您的代码可以简单得多。例如,假设您具有存储上次找到的位置的类/窗体级别变量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
}

最新更新