“查找下一个” RichTextBox 中的功能:包括“匹配大小写”
我知道这个问题已经被问过很多次了,但我有一个具体的要求。可能有点难以理解,对此我表示歉意。我会尽力对代码进行注释。如果您需要澄清,请告诉我。
我的问题是我有非常复杂、不必要的代码,因为我自己编写了它,这对我来说是一个新概念。该代码的目的是在主窗口的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用任何采用 RichTextBoxFinds 选项 - 它允许匹配按案件开/关。
您的代码可以简单得多。例如,假设您有类/表单级别变量
LastFoundLocation
来存储最后找到的位置。零值表示从头开始搜索,而-1表示从当前光标位置开始搜索。因此按钮处理程序中的相关 C# 代码是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