我可以向后浏览文本以在Visual C#中查找字符串吗?

嗅探

我有一个搜索功能,可以从文本的开头到结尾在Richtextbox中搜索一个字符串。当我找到最后一个符合条件时,它从头开始。

这里的代码:

       #region Search

      private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
    {
        start = 0;
        end = 0;
    }     

    //Searchfield
    private void toolStripTextBoxSearch_Click(object sender, EventArgs e)
    {

    }//end TextBoxSearch

    public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
    {
        // Set the return value to -1 by default.
        int retVal = -1;

        // A valid starting index should be specified.
        if (searchStart >= 0)
        {
            // A valid ending index
            if (searchEnd > searchStart || searchEnd == -1)
            {
                // Find the position of search string in RichTextBox
                indexOfSearchText = richTextBox.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
                // Determine whether the text was found in richTextBox1.
                if (indexOfSearchText != -1)
                {
                    // Return the index to the specified search text.
                    retVal = indexOfSearchText;
                }
            }
        }
        return retVal;
    }//end FindMyText

    private void buttonSearch_left_Click(object sender, EventArgs e)
    {

    }

    private void buttonSearch_right_Click(object sender, EventArgs e)
    {
        int startindex = 0;

        if (txtSearch.Text.Length > 0)
        {
             startindex = FindMyText(txtSearch.Text, start, richTextBox.Text.Length);
        }
            // If string was not found report it
            if (startindex < 0 )
            {
                if (stringfoundflag==1)
                {
                startindex = FindMyText(txtSearch.Text, 0, richTextBox.Text.Length); //Start at Pos. 0
                }
                else
                {
                    MessageBox.Show("Not found in Textfield", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }//end if 

             if (startindex >= 0)
            {
                stringfoundflag = 1;
            // Set the highlight color as red
            //richTextBox.SelectionColor = Color.Red;
            // Find the end index. End Index = number of characters in textbox
            int endindex = txtSearch.Text.Length;
                // Highlight the search string
                richTextBox.Select(startindex, endindex);
                // mark the start position after the position of
                // last search string
                start = startindex + endindex;
        }
    }

    // Reset the richtextbox when user changes the search string
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        start = 0;
        indexOfSearchText = 0;
    }

    private void txtSearch_Enter(object sender, EventArgs e)
    {
        if (txtSearch.Text == "Search")
        {
            txtSearch.Text = "";
        }
    }

    private void txtSearch_Leave(object sender, EventArgs e)
    {
        if (txtSearch.Text == "")
        {
            txtSearch.Text = "Search";
        }
    }

    #endregion

我有两个按钮,一是向右搜索,一是向左搜索,右边的功能就像开始时的描述一样。

问题:

我可以使用解决方案从右向左反转搜索功能,还是必须更改整个搜索功能?

示例:从第一个一致开始,然后跳到最后一个一致,然后从第二个跳到最后一个,依此类推。

嗅探

我找到了解决问题的方法。

代码:

        public int FindMyTextleft(string txtToSearch, int searchStart, int searchEnd)
    {
        // Set the return value to -1 by default.
        int retVal = -1;

        // A valid starting index should be specified.
        if (searchStart >= 0)
        {
            // A valid ending index
            if (searchEnd > searchStart || searchEnd == -1)
            {
                // Find the position of search string in RichTextBox
                indexOfSearchText = richTextBox.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.Reverse);
                // Determine whether the text was found in richTextBox1.
                if (indexOfSearchText != -1)
                {
                    // Return the index to the specified search text.
                    retVal = indexOfSearchText;
                }
            }
        }//end FindMyTextleft
        return retVal;
    }//end FindMyText

    private void buttonSearch_left_Click(object sender, EventArgs e)
    {
        int startindex = 0;

        if (txtSearch.Text.Length > 0 & stringfoundflag == 1)
        {
            startindex = FindMyTextleft(txtSearch.Text, startindex, end);
        }

        else if (txtSearch.Text.Length > 0)
        {
            startindex = FindMyTextleft(txtSearch.Text, start, richTextBox.Text.Length);
        }
        // If string was not found report it
        if (startindex < 0)
        {
            if (stringfoundflag == 1)
            {
                startindex = FindMyTextleft(txtSearch.Text, 0, richTextBox.Text.Length); //Start at Pos. 0
            }
            else
            {
                MessageBox.Show("Not found in Textfield", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }//end if 

        if (startindex >= 0)
        {
            stringfoundflag = 1;
            // Set the highlight color as red
            //richTextBox.SelectionColor = Color.Red;
            // Find the end index. End Index = number of characters in textbox
            int endindex = txtSearch.Text.Length;
            // Highlight the search string
            richTextBox.Select(startindex, endindex);
            // mark the start position after the position of
            // last search string
            end = startindex;
        }
    }//buttonSearch_left_Click

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

我可以向后浏览文本以在Visual C#中查找字符串吗?

来自分类Dev

我的方法可用于查找字符串中的每个单词吗

来自分类Dev

在文本部分中查找字符串

来自分类Dev

查找字符串并在Python中插入文本

来自分类Dev

查找字符串中不存在的文本

来自分类Dev

在Excel公式文本中查找字符串

来自分类Dev

查找字符串并在Python中插入文本

来自分类Dev

SQL查找字符串中的文本

来自分类Dev

Excel在文本中查找字符串

来自分类Dev

使用 postgresql 在大文本中查找字符串

来自分类Dev

我可以使用正则表达式在Resharper的自定义模式中查找字符串吗?

来自分类Dev

Oracle SQL查找字符串并向后工作

来自分类Dev

在文件中查找字符串并在bash中删除其旁边的所有文本

来自分类Dev

如何在JAVA中的文本文件中查找字符串

来自分类Dev

我可以使用方便的语法在哈希图中查找字符串键吗?

来自分类Dev

我可以使用方便的语法在哈希图中查找字符串键吗?

来自分类Dev

在大文本文件中查找字符串中子字符串的最后一次出现

来自分类Dev

Unix命令在文件中查找字符串,并仅打印带有字符串的模式文本

来自分类Dev

BATCH-在文本文件中查找字符串,然后在该行之后添加新的字符串

来自分类Dev

在文本文档行中查找字符串

来自分类Dev

在巨大的文本文件中查找字符串

来自分类Dev

如何从文本文件中查找字符串并加载其行内容

来自分类Dev

如何使用Google脚本查找字符串中的文本?

来自分类Dev

使用VLOOKUP在单元格中查找字符串文本

来自分类Dev

查找字符串中的文本,不区分大小写

来自分类Dev

在动态文本框JQUERY中查找字符串

来自分类Dev

批处理-在文本文件中查找字符串并删除整行

来自分类Dev

我有一个程序来查找字符串中字母的数量,但是它不完整,您能完成它吗python

来自分类Dev

Python-在文本文件中查找字符串列表的字符串频率

Related 相关文章

  1. 1

    我可以向后浏览文本以在Visual C#中查找字符串吗?

  2. 2

    我的方法可用于查找字符串中的每个单词吗

  3. 3

    在文本部分中查找字符串

  4. 4

    查找字符串并在Python中插入文本

  5. 5

    查找字符串中不存在的文本

  6. 6

    在Excel公式文本中查找字符串

  7. 7

    查找字符串并在Python中插入文本

  8. 8

    SQL查找字符串中的文本

  9. 9

    Excel在文本中查找字符串

  10. 10

    使用 postgresql 在大文本中查找字符串

  11. 11

    我可以使用正则表达式在Resharper的自定义模式中查找字符串吗?

  12. 12

    Oracle SQL查找字符串并向后工作

  13. 13

    在文件中查找字符串并在bash中删除其旁边的所有文本

  14. 14

    如何在JAVA中的文本文件中查找字符串

  15. 15

    我可以使用方便的语法在哈希图中查找字符串键吗?

  16. 16

    我可以使用方便的语法在哈希图中查找字符串键吗?

  17. 17

    在大文本文件中查找字符串中子字符串的最后一次出现

  18. 18

    Unix命令在文件中查找字符串,并仅打印带有字符串的模式文本

  19. 19

    BATCH-在文本文件中查找字符串,然后在该行之后添加新的字符串

  20. 20

    在文本文档行中查找字符串

  21. 21

    在巨大的文本文件中查找字符串

  22. 22

    如何从文本文件中查找字符串并加载其行内容

  23. 23

    如何使用Google脚本查找字符串中的文本?

  24. 24

    使用VLOOKUP在单元格中查找字符串文本

  25. 25

    查找字符串中的文本,不区分大小写

  26. 26

    在动态文本框JQUERY中查找字符串

  27. 27

    批处理-在文本文件中查找字符串并删除整行

  28. 28

    我有一个程序来查找字符串中字母的数量,但是它不完整,您能完成它吗python

  29. 29

    Python-在文本文件中查找字符串列表的字符串频率

热门标签

归档