Search and Highlight Text in a RichTextBox

Rate this post

tep 1: Create a new Windows Forms application. Drag and drop a RichTextBox(rtb) control, a textbox(txtSearch) and a button(btnFind) control to the form. The textbox will be used to enter the search string and on the button click, we will be performing a search in the RichTextBox. Add a class level variable called ‘start’. This variable will be the start position within the control’s text from where you need to begin searching. Also add another variable called ‘indexOfSearchText’ to hold the position/index of the search string in the RichTextBox.
C#

[code]
      int start = 0;
      int indexOfSearchText = 0;
[/code]

Step 2: In the button click event, add the following code. The FindMyText() function, which we will visit in Step 3, is called by passing the search string, the start and the end index. If the string is found in the RichTextBox, the position of the search string is returned and highlighted. The function has been marked with appropriate comments to help you understand the code. C#

[code]
        privatevoid btnFind_Click(object sender, EventArgs e)
        {
            int startindex = 0;
 
            if(txtSearch.Text.Length > 0)
                startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length);
 
            // If string was found in the RichTextBox, highlight it
            if (startindex >= 0)
            {
                // Set the highlight color as red
                rtb.SelectionColor = Color.Red;
                // Find the end index. End Index = number of characters in textbox
                int endindex = txtSearch.Text.Length;
                // Highlight the search string
                rtb.Select(startindex, endindex);
                // mark the start position after the position of
                // last search string
                start = startindex + endindex;
            }
        }
[/code]

Step 3: Now let us add the functionality to the FindNext() function. The function accepts the search string, and the start and end index. The search happens within the index specified. The validation occurs first where the start and end index are checked for valid values. We then use the Find() function of the RichTextBox to find the position of the search string in the RichtTextBox. If the string is found, the position of the string within the RichTextBox is returned. If the string is not found, -1 is returned. The function has been marked with appropriate comments to help you understand the code. C#

[code]
       publicint FindMyText(string txtToSearch, int searchStart, int searchEnd)
        {
            // Unselect the previously searched string
            if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
            {
                rtb.Undo();
            }
 
            // Set the return value to -1 by default.
            int retVal = -1;
 
            // A valid starting index should be specified.
            // if indexOfSearchText = -1, the end of search
            if (searchStart >= 0 && indexOfSearchText >=0)
            {
                // A valid ending index
                if (searchEnd > searchStart || searchEnd == -1)
                {
                    // Find the position of search string in RichTextBox
                    indexOfSearchText = rtb.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;
        }
[/code]

Step 4: If a user changes the search string in the textbox, you can handle the TextChanged event to reset the value of ‘start’ and ‘indexOfSearchText’ to zero. This starts the new search from the beginning. C#

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

[code]<a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=146" rel="noopener" target="_blank">dotnetcurry</a>[/code]
بیشتر بخوانید:   نکات و ترفندهای جستجو در گوگل

Check Also

c# Shrp Programing

تغییر زبان اتوماتیک بصورت اتوماتیک از فارسی به انگلیسی و برعکس در نرم افزار

فرض کنید که دو نرم افزاری که دارید مینویسید چند تا   TextBox بر روی فرم …

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *