首页 > 解决方案 > C#; 将滚动条移动到搜索词的位置

问题描述

我编写了在richtextbox 中查找单词的代码。现在,我想将滚动条移动到搜索词的位置。请帮我!!!

 int j,count = 0;
        string keyword = txtSearchByWords.Text;
        int startPosition = 0; //initializing starting position to search
        int endPosition = 0;
        int endArticle = rtbWords.Text.Length;
        for (j = 0; j < endArticle; j = startPosition)//creating a loop until ending the article
        {
            if (j == -1) //if the loop goes to end of the article then stop
            {
                break;
            }
            startPosition = rtbWords.Find(keyword, startPosition, endArticle, RichTextBoxFinds.WholeWord);
            if (startPosition >= 0)     //if match the string                                                         
            {
                count++;//conunting the number of occuerence or match the search string
                rtbWords.SelectionBackColor = Color.Orange;//coloring the matching string in the article
                endPosition = txtSearchByWords.Text.Length;
                startPosition = startPosition + endPosition;//place the starting position at the next word of previously matching string to continue searching.
            }

标签: c#findscrollbar

解决方案


使用 ScrollToCaret 滚动到富文本框中搜索词的位置

rText.SelectionStart = rtext.Text.Length;//set the start location
rText.ScrollToCaret();

推荐阅读