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

发布于 2024-11-09 03:10:36 字数 3127 浏览 0 评论 0原文

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

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

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

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

I know that this question has been asked loads of times, but I have a specific request. It might be a bit hard to understand, for which I apologize. I'll try and comment the code as best I can. If you need clarification, please tell me.

My problem is that I have very complicated, unnecessary code because I wrote it myself and this is a new concept to me. The code is intended to find a string in the main window's RichTextBox (the string is obviously supplied by the user). The code works fine, but due to its convoluted nature, I don't know how to implement a "Match case" function.

The form has these controls (at least the ones you use when searching for text):

  • Textbox: txtFind - User enters a search term here
  • Checkbox: chkMatchCase - Allows a user to choose whether or not to match the case of the search term
  • Button: btnFind - The Find Next button

And this is my code:

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

Anyway, if there's a better way to implement this, please let me know. And I did extensive searching, but found no good solution. I ended up combining a bunch of little parts of other tutorials into this. If there's no better solution, I'd just like to know how to implement a "Match case" function.

Sorry for the trouble and thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪都笑了 2024-11-16 03:10:36

使用任何采用 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
}

Use any overload of Find method that takes RichTextBoxFinds option - it allows to matching by case on/off.

Your code can be a lot simpler. For example, assume that you have class/form level variable LastFoundLocation that stores the last found location. The zero value means search from start while -1 means search from current cursor location. So relevant C# code in the button handler would be

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文