开发者

C# RichTextBox Select specified Text

开发者 https://www.devze.com 2023-03-31 05:04 出处:网络
I have a RichTextBox with -by example- this text: \"This is my Text\" Now I want to \"search\" the RichTextBox for a Text (String), by example:

I have a RichTextBox with -by example- this text:

"This is my Text"

Now I want to "search" the RichTextBox for a Text (String), by example:

"Text"

Now "Text" should be selected/highlighted (for each one) in the RichTextBox..

There is something like:

myRichTextBox.Select();

but here I have to set a StartPosition and so on, but I want to search for String!

开发者_如何学C

How could I do this? (Searched stackoverflow, didn't find something similiar..)


     int start = 0;
int indexOfSearchText = 0;
private void 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;
            }
        }




public int 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;
        }



private void textBox1_TextChanged(object sender, EventArgs e)
        {
            start = 0;
            indexOfSearchText = 0;
        }

CheckOut this article if you dont understand this code... http://www.dotnetcurry.com/ShowArticle.aspx?ID=146


You can only have one selection in a text box. What you want is to highlight the found text.
You could achieve it like this:

  1. Find the positions of the text you want to highlight using repeated calls to myRichTextBox.Text.IndexOf with the last found index + 1 as the start position.
  2. Highlight the found texts using the default RichTextBox capabilities.


You can use the Find method to find the startindex of your searched text:

int indexToText = myRichTextBox.Find(searchText);

You can then use this index and the Select method to select the text:

int endIndex = searchText.Length;
myRichTextBox.Select(indexToText , endIndex);


    private void Txt_Search_Box_TT_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
        FOFO:
            int start =
                RtfAll_Messages.Find(Txt_Search_Box_TT.Text, RtfAll_Messages.SelectionStart + 1,
                RichTextBoxFinds.None);
            if (start >= 0)
                RtfAll_Messages.Select(start, Txt_Search_Box_TT.Text.Length);
            else
            {
                start = 0;
                RtfAll_Messages.SelectionStart = 0;
                RtfAll_Messages.SelectionLength = 0;
            }
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号