C# 创建查找下一个函数

发布于 2024-10-06 05:05:54 字数 654 浏览 4 评论 0原文

如何创建查找下一个函数?

从我当前的代码来看,如果在 richTextBoxBrowsing 中找到 textBoxSearch 中的单词,则该单词将在 richTextBoxBrowsing 内突出显示。

但如果找到多个相同的单词,我只能看到第一个。因此,我想按F3按钮查找下一个单词,它会被一个一个突出显示,直到richTextBoxBrowsing结束。

提前致谢!

        String s1 = textBoxSearch.Text.ToLower();
        int startPos = richTextBoxBrowsing.Find(s1);
        int length = s1.Length;

        if (startPos > -1)
        {
            MessageBox.Show("Word found!");
            richTextBoxBrowsing.Focus();
            richTextBoxBrowsing.Select(startPos, length);
        }

        else
        {
            MessageBox.Show("Word not found!");
        }

How can I create a find next function?

From my current codes, if a word from textBoxSearch is found richTextBoxBrowsing, the word would be highlighted inside the richTextBoxBrowsing.

But if there are more than 1 of the same word found, I can only see the first one. Hence, I would like to press the button F3 to find the next word and it would be highlighted one by one until the end of richTextBoxBrowsing.

Thanks in advance!

        String s1 = textBoxSearch.Text.ToLower();
        int startPos = richTextBoxBrowsing.Find(s1);
        int length = s1.Length;

        if (startPos > -1)
        {
            MessageBox.Show("Word found!");
            richTextBoxBrowsing.Focus();
            richTextBoxBrowsing.Select(startPos, length);
        }

        else
        {
            MessageBox.Show("Word not found!");
        }

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

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

发布评论

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

评论(3

寻找一个思念的角度 2024-10-13 05:05:54

诀窍是保留最后一个已知索引(即您为 startPos 获得的最后一个值) - 也许在表单级字段中,然后您可以使用:(

int startPos = Find(s1, lastIndex + 1, RichTextBoxFinds.None);

其中 lastIndex< /code> 为 -1 将导致它从头开始)

The trick is to keep hold of the last known index (i.e. the last value you got for startPos) - perhaps in a form-level field, then you can use:

int startPos = Find(s1, lastIndex + 1, RichTextBoxFinds.None);

(where a lastIndex of -1 will cause it to start at the beginning)

愛上了 2024-10-13 05:05:54

您必须保存先前搜索的状态,例如记住先前找到的项目的索引。每当搜索字符串发生更改时,您都将起始索引重置为 -1。

You'd have to save the state of the previous search, for instance the remember the index of the previously found item. Whenever the search string changes, you reset the starting index to -1.

爱本泡沫多脆弱 2024-10-13 05:05:54

这是我所拥有的“查找下一个”功能。它是在 VB.net 中,因为我目前正在开发一个 TAFE 项目,但您可以轻松地将其转换为 C#。这对我来说有奇效。

我有一个名为“RichTextBox1”的主富文本框,其中包含文本,然后有一个名为“ToolStripSearchTextBox”的文本框,我在其中输入要搜索的内容,还有一个名为“ToolStripButton2”的按钮,单击时调用方法“FindNext_Click()”。

由于“RichTextBoxFinds.None”,此“查找下一个”功能不区分大小写。您可以随意更改它。

// Find next
Dim searchIndex As Integer = 0
Dim lastSearch As String

Private Sub FindNext_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
    // If the search textbox is empty, focus on it
    If (ToolStripSearchTextBox.Text = String.Empty) Then
        ToolStripSearchTextBox.Focus()
        Return
    End If
    // If user changed their search term, reset the index
    If (ToolStripSearchTextBox.Text <> lastSearch) Then
        searchIndex = 0
    End If
    lastSearch = ToolStripSearchTextBox.Text

    // If the character(s) exist, update the index. Otherwise, set the index to -1
    Try
        searchIndex = RichTextBox1.Find(ToolStripSearchTextBox.Text, searchIndex, RichTextBoxFinds.None)
    Catch ex As ArgumentOutOfRangeException
        searchIndex = -1
    End Try

    // Character(s) exists, focus on the main textbox and then select the character(s)
    If (searchIndex <> -1) Then
        RichTextBox1.Focus()
        RichTextBox1.SelectionStart = searchIndex
        RichTextBox1.SelectionLength = ToolStripSearchTextBox.Text.Length

        searchIndex = searchIndex + 1
    Else // No occurances of text or user has highlghted last remaining word. Let the user know they have reached the end of the document and reset the index
        searchIndex = 0
        //RichTextBox1.SelectionStart = 0
        //RichTextBox1.SelectionLength = 0
        MessageBox.Show("End of document", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
End Sub

Here's what I've got for a "Find Next" function. It's in VB.net as I'm currently working on a TAFE project but you can easily convert it to C#. It works wonders for me.

I have a main richtextbox called 'RichTextBox1' where the text is, then I have a textbox called 'ToolStripSearchTextBox' where I input what I want to search and a button called 'ToolStripButton2' that calls the method 'FindNext_Click()' when clicked.

This "Find Next" function is not case-sensitive due to 'RichTextBoxFinds.None'. Feel free to change that as you wish.

// Find next
Dim searchIndex As Integer = 0
Dim lastSearch As String

Private Sub FindNext_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
    // If the search textbox is empty, focus on it
    If (ToolStripSearchTextBox.Text = String.Empty) Then
        ToolStripSearchTextBox.Focus()
        Return
    End If
    // If user changed their search term, reset the index
    If (ToolStripSearchTextBox.Text <> lastSearch) Then
        searchIndex = 0
    End If
    lastSearch = ToolStripSearchTextBox.Text

    // If the character(s) exist, update the index. Otherwise, set the index to -1
    Try
        searchIndex = RichTextBox1.Find(ToolStripSearchTextBox.Text, searchIndex, RichTextBoxFinds.None)
    Catch ex As ArgumentOutOfRangeException
        searchIndex = -1
    End Try

    // Character(s) exists, focus on the main textbox and then select the character(s)
    If (searchIndex <> -1) Then
        RichTextBox1.Focus()
        RichTextBox1.SelectionStart = searchIndex
        RichTextBox1.SelectionLength = ToolStripSearchTextBox.Text.Length

        searchIndex = searchIndex + 1
    Else // No occurances of text or user has highlghted last remaining word. Let the user know they have reached the end of the document and reset the index
        searchIndex = 0
        //RichTextBox1.SelectionStart = 0
        //RichTextBox1.SelectionLength = 0
        MessageBox.Show("End of document", String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文